0

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" 
3
  • did you try using Chr(34) to get the "" ? try "WshShell.Run" & Chr(34) & "C:\Users\John Doe\test.exe" & Chr(34) Commented Jun 17, 2017 at 16:55
  • 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! Commented Jun 17, 2017 at 17:12
  • 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""" Commented Jun 17, 2017 at 18:42

1 Answer 1

1

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 & """"""" "
'                                        ^^^^               ^^^^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.