Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Peony
Creator III
Creator III

VBS. openOutputFile issue

Hi all.

Here is the macro to save variables from dashboard into particular .csv file.

Whan i run it as button-push action it opens "EditModule" window at the line: Set objTextFile = openOutputFile(fileName & ".var.csv"). Please help to understend what is wrong with code?

Sub extractVars

    fileName= replace(ActiveDocument.GetProperties.FileName,".qvw","")

    Set objTextFile = openOutputFile(fileName & ".var.csv")

    objTextFile.WriteLine("VariableName|VariableValue")

    Set vars = ActiveDocument.GetVariableDescriptions

    for i = 0 to vars.Count - 1

        set v = vars.Item(i)

        rem exclude IsConfig & IsReserved vars

       If Not (v.IsReserved Or v.IsConfig) Then

           shownValue = v.ShownValue.String

           objTextFile.WriteLine( csvQuote(v.Name) & "|" & csvQuote(v.RawValue))

        End If

    next

    objTextFile.Close()

    msgbox("Vars saved:"&vars.Count)

End Sub

1 Solution

Accepted Solutions
marcus_sommer

You need to create a FileSystemObject for it. Here an example how it could be done: Re: Export to CSV with quotation marks.

- Marcus

View solution in original post

4 Replies
marcus_sommer

You need to create a FileSystemObject for it. Here an example how it could be done: Re: Export to CSV with quotation marks.

- Marcus

Peony
Creator III
Creator III
Author

Marcus, thank you for your advice. I've add it as:

Set fso = CreateObject("Scripting.FileSystemObject")

Set objTextFile = fso.openOutputFile(fileName & ".var.csv")

But the problem is still stay.

marcus_sommer

It should be:

Set objTextFile = fso.OpenTextFile(fileName & ".var.csv", 2, true)

To the meaning of the second/third parameter just put the keyword into google and you will find a lot of examples.

- Marcus

Peony
Creator III
Creator III
Author

Thank you again. I made changes, so now macro is works Here it is:

Sub a1

dim fso, fileName, filePath, fileVar, vars, storageFile

fileName = replace(ActiveDocument.GetProperties.FileName,".qvw","")

filePath = (fileName)&".var.csv"

    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FileExists(filePath) Then

        Set storageFile = fso.OpenTextFile (filePath, 2, True)

    Else

        Set storageFile = fso.CreateTextFile(filePath)

    End If

    Set vars = ActiveDocument.GetVariableDescriptions

    for i = 0 to vars.Count - 1

        set v = vars.Item(i)

        If Not (v.IsReserved Or v.IsConfig) Then

        storageFile.WriteLine (v.Name & "|" & v.RawValue)

        End If

    next

    storageFile.Close()

    msgbox("Vars saved:"&vars.Count)

End Sub