Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Defining Variables outside a Sub (marco variable)

Hi all,

Is it possible to define a variable, to be used in sub / macro?

I have a file path hardcoded in a macro, but if I want this to change, I don't really want to search the script for all instances of it.

I know in SAS, SQL etc...I can use a macro variable & state it at the top.  Is this possible with VBScript?

Something like this I want to do:

FilePath = C:\user\document\pictures

Sub

dim fso
Set fso =CreateObject("Scripting.FilesystemObject")
fso.CreateTextFile "FilePath\dummy.txt"

End Sub

1 Solution

Accepted Solutions
tamilarasu
Champion
Champion

Hi Jammy,

Create a variable in front end and assign the variable in inputbox. You can change the path whenever required,

Untitled.png

You can use below code to fetch the path from front end variable.

Sub Test

Dim fsoh

FilePath = ActiveDocument.GetVariable("vPath").GetContent.String

FileDate =  ActiveDocument.Evaluate("date(Now(), 'DDMMYYYY hhmmss')")

If Right(FilePath,1)<> "\" then

FilePath = FilePath & "\"

End If

Set fso =CreateObject("Scripting.FilesystemObject")

fso.CreateTextFile FilePath & "dummy_" & FileDate & ".txt"

End Sub

Sample attached fro your reference.

View solution in original post

3 Replies
tamilarasu
Champion
Champion

Hi Jammy,

Create a variable in front end and assign the variable in inputbox. You can change the path whenever required,

Untitled.png

You can use below code to fetch the path from front end variable.

Sub Test

Dim fsoh

FilePath = ActiveDocument.GetVariable("vPath").GetContent.String

FileDate =  ActiveDocument.Evaluate("date(Now(), 'DDMMYYYY hhmmss')")

If Right(FilePath,1)<> "\" then

FilePath = FilePath & "\"

End If

Set fso =CreateObject("Scripting.FilesystemObject")

fso.CreateTextFile FilePath & "dummy_" & FileDate & ".txt"

End Sub

Sample attached fro your reference.

tamilarasu
Champion
Champion

If you want to hardcode the path use below code.

FilePath = "C:\user\document\pictures"

Sub Test

Dim fso

Set fso =CreateObject("Scripting.FilesystemObject")

fso.CreateTextFile FilePath & "\Dummy.txt"

End Sub

Not applicable
Author

Thank you..... perfect solution & I got it working!