来自 VBA 的整个 Shell 命令的变量

Variable as the entire Shell command from VBA

我有一个 shell 命令来执行 VBScript,当它全部作为字符串文字输入时,它可以工作:

作品:

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

但是如果我构建要放入 shell 的字符串并将其作为变量,它不会:

不工作:

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)

我正在尝试从 VBA 模块中 运行 它。感谢您的帮助!

尝试从字符串的开头删除 Chr(34):

来自:

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)

至:

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)

不需要那么多杂耍...

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

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

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

对我来说是最易读的方法。