This document will ask you for the pathname to a source QlikView document containing the variables you want to copy. Then it will ask for the pathname to a destination document that will have the copied variables inserted. If either of the documents requires a password you will be asked for the password as the macro opens the documents. There's no error checking but the most common error for this macro is entering an invalid pathname. After the variables are copied, the macro will ask if it is OK to save the destination document. Remember, as always, to make a backup copy of your documents before testing macros like this.
Here's the code that you can copy and paste into your own toolkit document:
Sub Copy_Variables_Doc_to_Doc
'----------------------------
' /* Copy Variables From One QV Document to Another */
' /* QlikView Maven, February 2013 */
Dim objQV, objSource, objDest, objSourceVar, objDestVar
Dim objVars, varcontent, objTempVar, varname, i, varctr
'initialize
fn_source=inputbox("Enter pathname to source document containing the variables you want to copy:","Enter Source Document Pathname")
if trim(fn_source)="" then 'no entry or cancel
exit sub
end if
Set objSource=Application.OpenDoc(fn_source)
fn_dest=inputbox("Enter pathname to destination document where variables are to be inserted:","Enter Destination Document Pathname")
if trim(fn_dest)="" then 'no entry or cancel
exit sub
end if
Set objDest=Application.OpenDoc(fn_dest)
'Loop through the variables
set objVars=objSource.GetVariableDescriptions
varctr=0
for i = 0 to objVars.Count - 1
varctr=varctr+1
set objTempVar = objVars.Item(i)
varname=Trim(objTempVar.Name)
Set objSourceVar=objSource.Variables(varname)
varcontent=objSourceVar.GetRawContent
'display the variable to check on progress if needed
'msgbox(varname & " = " & varcontent)
'update the value of variable in destination document
Set objDestVar=objDest.Variables(varname)
If objDestVar is nothing then
'must need to create variable
objDest.CreateVariable varname
Set objDestVar=objDest.Variables(varname)
End If
objDestVar.SetContent varcontent,true
next 'end of loop
'we're done, show a message and close down
x=msgbox(varctr&" variables copied from "&fn_source&" to "&fn_dest&"."&chr(13)&"OK to Save?",vbOKCancel,"Copy_Variables_Doc_to_Doc")
if x=vbOK then
ObjDest.Save
End if
objDest.CloseDoc 'comment out this line if you'd like dest document to stay open
objSource.CloseDoc
set objSource=nothing
set objDest=nothing
set objVars=nothing
set objTempVar=nothing
set objSourceVar=nothing
set objDestVar=nothing
End Sub 'end of Copy_Variables_Doc_to_Doc
★★★
2 comments:
For anyone interested, here is the code for the macro with browse-for-file functionality. This only works if you have MS Office installed or MS Excel for Windows because it borrows an object method from the Excel object model. You will also need to set Module Security and Local Security to Allow System Access (on the left side of your Edit Module window).
Sub Copy_Variables_Doc_to_Doc
'----------------------------
' /* Copy Variables From One QV Document to Another */
' /* Requested Module Security and Current Local Security must be set to Allow System Access */
' /* QlikView Maven, February 2013 */
Dim objQV, objSource, objDest, objSourceVar, objDestVar, objXL
Dim objVars, varcontent, objTempVar, varname, i, varctr
'initialize
Set objXL=CreateObject("Excel.Application")
fn_source=objXL.GetOpenFilename("All Files (*.qvw),*.qvw",,"Select file containing variables you want to copy",False)
If fn_source="False" then
'user cancelled out of dialog box
Set oXL=nothing
Exit sub
End If
'fn_source=inputbox("Enter pathname to source document containing the variables you want to copy:","Enter Source Document Pathname")
'if trim(fn_source)="" then 'no entry or cancel
' exit sub
' end if
fn_dest=objXL.GetOpenFilename("All Files (*.qvw),*.qvw",,"Select document where variables are to be inserted",False)
If fn_dest="False" then
'user cancelled out of dialog box
Set oXL=nothing
Exit sub
End If
'fn_dest=inputbox("Enter pathname to destination document where variables are to be inserted:","Enter Destination Document Pathname")
'if trim(fn_dest)="" then 'no entry or cancel
' exit sub
' end if
Set objSource=Application.OpenDoc(fn_source)
Set objDest=Application.OpenDoc(fn_dest)
Set oXL=nothing
'Loop through the variables
set objVars=objSource.GetVariableDescriptions
varctr=0
for i = 0 to objVars.Count - 1
varctr=varctr+1
set objTempVar = objVars.Item(i)
varname=Trim(objTempVar.Name)
Set objSourceVar=objSource.Variables(varname)
varcontent=objSourceVar.GetRawContent
'display the variable to check on progress if needed
'msgbox(varname & " = " & varcontent)
'update the value of variable in destination document
Set objDestVar=objDest.Variables(varname)
If objDestVar is nothing then
'must need to create variable
objDest.CreateVariable varname
Set objDestVar=objDest.Variables(varname)
End If
objDestVar.SetContent varcontent,true
next 'end of loop
'we're done, show a message and close down
x=msgbox(varctr&" variables copied from "&fn_source&" to "&fn_dest&"."&chr(13)&"OK to Save?",vbOKCancel,"Copy_Variables_Doc_to_Doc")
if x=vbOK then
ObjDest.Save
End if
objDest.CloseDoc 'comment out this line if you'd like dest document to stay open for examination
objSource.CloseDoc
set objSource=nothing
set objDest=nothing
set objVars=nothing
set objTempVar=nothing
set objSourceVar=nothing
set objDestVar=nothing
End Sub 'end of Copy_Variables_Doc_to_Doc
thanks! saved me some work.
Post a Comment