Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

write variable to file using macro

Hey community,

I think I need a macro to do the following job:

create a "text file" with the content of a variable and open the file once the button has been clicked.

The variable contains static text plus two other variables.

I'm already struggling with outputting the actual values the variable holds inside a macro message box.

Example:

vtest = 1+1

Sub CheckVariable

    set vCheck = ActiveDocument.Variables("vTest")

    MsgBox(vCheck.GetContent.String)

End Sub 

This outputs 1+1 but how can I get it to output the result of 2 (like with the dollar sign expansion)?

Once this is solved how can write the content of the variable to a .customExtension file and execute the file to trigger the default program? Could this be some temporary file that is only used in memory or do I have to write it to a path?

Thank you,

Thorsten

4 Replies
bbi_mba_76
Partner - Specialist
Partner - Specialist

Hi,

you could try

Sub CheckVariable

    vCheck = ActiveDocument.Evaluate("=$(vtest)")

    MsgBox vCheck

End Sub 

bbi_mba_76
Partner - Specialist
Partner - Specialist

This to create a file

Sub writeFile

    DIM fso, MyFile, strFile, strText

    strFile="D:\YourFile.csv"   

    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(strFile) Then

        Set MyFile = fso.OpenTextFile (strFile, 8, True)

    Else

        Set MyFile = fso.CreateTextFile(strFile)

        MyFile.WriteLine("""Date""")

    End If

   

    strText= """" & Now() & ""

    MyFile.WriteLine(strText)

    MyFile.Close

End Sub 

Not applicable
Author

thank you Michele this works great.

So what I do is create the file with a static filename in c:\temp and have a second action launch this filename.

Is that the right way to go or can/should I launch the file from within the macro?

Not applicable
Author

Please have a look at the attached qvw.

Instead of creating the file in the temp folder I would like to launch the file or string directly from in memory so that I can avoid macro security issues when trying to write to disk.

Sub createFileAndOpen

   link = ActiveDocument.Evaluate("=$(vLink)")

    DIM fso, MyFile, strFile, strText

    strFile="c:\temp\link.msgFile" 

    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(strFile) Then

       Set MyFile = fso.OpenTextFile (strFile, 2, True)

    Else

        Set MyFile = fso.CreateTextFile(strFile)

    End If

      MyFile.WriteLine(link)

      OpenFile()

    MyFile.Close

End Sub

FUNCTION OpenFile

set app = ActiveDocument.GetApplication

app.Launch "C:\temp\link.msgFile", "file path"

END FUNCTION