Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
there is the possibility to export the macro-/module-code ...
Is there a similar possibility to import/include "external" macro-code with a statement like the include-statement within loading-scripts?
I would need this functionality to organize some code within in project with many modules/subs ...
Thanks in advance & best regards
Stefan
Stefan, Here's one way to do it.
1. Put your common code in a file, say "commonFunctions.vbs".
2. Add this macro code to your Qlikview Document module:
Call IncludeCommon
Sub IncludeCommon
Dim objFSO, file, str
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set file = objFSO.OpenTextFile("commonFunctions.vbs")
str = file.ReadAll
file.Close
ExecuteGlobal str
End Sub
Press the "Check" button. Bingo, your externally defined Functions and Subs are available. Your document must be granted system access for this to work.
Changes made to the external file will be picked up each time the document is opened. If you are developing and don't want to open/close the doc, making an edit change to the document module will pick up the changes as well.
Let me know if this works for you. I haven't actually tried this in production.
-Rob
Rob, thanks for your idea! I was able to get your code working with some slight changes. The working directory needs to be included in the code file. So you need to replace this line:
Set objFSO = CreateObject("Scripting.FileSystemObject")
with these 4 lines:
set mypath = ActiveDocument.GetProperties
directory = mypath.MyWorkingDirectory
codefile = directory & "\commonFunctions.vbs"
Set file = objFSO.OpenTextFile(codefile)
The only problem now is that the code is not password protected.
Does anyone have any suggestions on hiding this code?
I tried saving the QVW after I loaded the external code, but when I reopened it, the external code had not been saved.
I guess I could also encrypt the VBS files manually, but that's not ideal.
One method could be to create a Master .qvw containing the macros and password protect it, then use code in the destination document to read the master code and overwrite it. I have managed to do this as follows (all documents in same folder to simplify) ...
1) Create ModuleMaster.qvw and password protect. Module needs to include the import .vbs routine as above.
2) Create .vbs file with following code ...
Function GetScript(Doc)
set QV=CreateObject("QlikTech.QlikView")
set Doc = QV.OpenDoc (Doc)
set props = Doc.GetProperties
GetScript = props.VBModule
Doc.CloseDoc
End Function
Sub ModifyScript
set docprop = ActiveDocument.GetProperties
docprop.VBModule = GetScript("ModuleMaster.qvw")
ActiveDocument.SetProperties docprop
End Sub
3) In destination document, include just the import .vbs routine again, click check and the ModifyScript sub should be available.
4) Run the ModifyScript (enter ModuleMaster.qvw credentials if needed) and the script should be overwritten. As we have included the import .vbs routine in ModuleMaster, it is retained for future use.
A word of warning though, I did observe that on one test my module wouldn't display the subs after "Check"-ing and I had to close and restart QV, but hopefully this could give some ideas.
flipside