Steve Lang
Steve Lang

Reputation: 7

Variable as the entire Shell command from VBA

I have a shell command to execute a VBScript, that works when it's all typed in as a string literal:

WORKS:

Shell("cscript.exe ""C:\Users\slang\Documents\Folio\test.vbs"" ""C:\Users\slang\Documents\Folio"" ""hbs110.fff"" ")

But if I build the string being placed into the shell and have it as a variable, it doesn't:

DOES NOT WORK:

strVBSPath = "C:\Users\slang\Documents\Folio\test.vbs"
strParentPath = "C:\Users\slang\Documents\Folio" 
strFFF = "hbs110.fff"
strShellScript = Chr(34) & "cscript.exe " & Chr(34) & Chr(34) & strVBSPath & Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strParentPath & _
        Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strFFF & Chr(34) & Chr(34) & " " & Chr(34)


Shell (strShellScript)

I'm trying to run it from a VBA module. Any help is appreciated!

Upvotes: 0

Views: 218

Answers (3)

Steve Lang
Steve Lang

Reputation: 7

Dim args
args = Array("cscript.exe", _
             "'" & strVBSPath & "'", _
             "'" & strFFFPath & "'", _
             strFFF)

Shell Replace(Join(args, " "), "'", """")

For me is the most readable method.

Upvotes: -1

Tim Williams
Tim Williams

Reputation: 166885

No need for all that juggling...

Dim args
args = Array("cscript.exe", _
             "'C:\Users\slang\Documents\Folio\test.vbs'", _
             "'C:\Users\slang\Documents\Folio'", _
             "hbs110.fff")

Shell Replace(Join(args, " "),"'", """")

Upvotes: 2

szewski
szewski

Reputation: 109

Try to remove Chr(34) from the beginning of your string:

from:

strShellScript = Chr(34) & "cscript.exe " & Chr(34) & Chr(34) & strVBSPath & Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strParentPath & _
        Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strFFF & Chr(34) & Chr(34) & " " & Chr(34)

to:

strShellScript = "cscript.exe " & Chr(34) & Chr(34) & strVBSPath & Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strParentPath & _
        Chr(34) & Chr(34) & " " & Chr(34) & Chr(34) & strFFF & Chr(34) & Chr(34) & " " & Chr(34)

Upvotes: 0

Related Questions