0
votes

I'm stuck on writing a VBScript file that can call an executable. Stuck on syntax for double-quotes in string literal.

This line is supposed to correctly write the line that calls the executable:

Print #PayLoadFile, "     WshShell.Run """ & exePath & """ "

exePath is variable holding path to executable, and it is correct.

Trying to get the line above to write to the vbs with the following:

WshShell.Run """C:\Users\John Doe\test.exe"""

When I run VBScript manually editing the file with """ between the executable, I do get correct results.

But instead it writes it as it gets error of System cannot find file specified:

WshShell.Run "C:\John Doe\test.exe" 
1
did you try using Chr(34) to get the "" ? try "WshShell.Run" & Chr(34) & "C:\Users\John Doe\test.exe" & Chr(34)Shai Rado
So, what is the problem you're having? You also say you get correct results........ Try like WshShell.Run """C:\Users\John Doe\test.exe""", Use three double quotes in one side here!GTAVLover
I tried the Chr(34) and it didn't work. The problem I'm having, let me clarify. When I do the command inside of vbshell with three double quotes, it works as intended. The problem: I have parent code inside of a VBA Macro function in Word, and I'm trying to have the Macro function write the vbscript file to file system (completely separate from VBA), so that it has written the line that says: WshShell.Run"""C:\Users\John Doe\test.exe"""Robert

1 Answers

1
votes

In VBScript double quotes inside string literals must be escaped, because the string literal itself must be enclosed in a pair of double quotes. The escaping is done by doubling the double quote. Hence the VBA statement

Print #PayLoadFile, "     WshShell.Run """ & exePath & """ "
'                                      ^^               ^^    these double quotes

creates a VBScript statement with the path in a single set of double quotes:

WshShell.Run "C:\Users\John Doe\test.exe" 
'            ^                          ^                     become these double quotes

To get the required additional two pairs of double quotes in the VBScript file (that will put the path in double quotes for the shell) you need to add 8 more double quotes to the VBA statement:

Print #PayLoadFile, "     WshShell.Run """"""" & exePath & """"""" "
'                                        ^^^^               ^^^^