Excel 使用';wsh.Run';在VBA中,文件路径中带有空格

Excel 使用';wsh.Run';在VBA中,文件路径中带有空格,excel,vba,escaping,filepath,Excel,Vba,Escaping,Filepath,我在使用WSH.run命令和转义文件路径中的空格时遇到了问题-没有空格它可以正常工作!我曾尝试在每个参数/文件路径周围使用双引号、三引号和四引号,但它不喜欢从VBA脚本调用cmd shell时的文件路径。这是没有任何转义的脚本: Dim strFilePath As String Dim xslFilePath As String Dim RetVal Dim wsh As Object Set wsh = VBA.CreateObject("WScript.Shell") firstFil

我在使用WSH.run命令和转义文件路径中的空格时遇到了问题-没有空格它可以正常工作!我曾尝试在每个参数/文件路径周围使用双引号、三引号和四引号,但它不喜欢从VBA脚本调用cmd shell时的文件路径。这是没有任何转义的脚本:

Dim strFilePath As String
Dim xslFilePath As String
Dim RetVal

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")

firstFilePath = "C:\Program Files\myprogram.exe"
secondFilePath = "C:\Program Files\stylesheet.xsl"

strfilepath = "D:\outputfiles\out.xls"
outpath = Mid(strFilePath, InStrRev(strFilePath, "\") + 1)
outfile = Left(strFilePath, InStrRev(strFilePath, ".") - 1)
logfile = outfile & "_errors.log"

cmd1 = firstFilePath & " -s:" & strFilePath & " -xsl:" & secondFilePath & " -o:" & outfile & ".csv > " & logfile & " 2>&1"

wsh.Run "cmd /c /s" & cmd1, 2, True
当我使用转义文件路径通过命令行运行以下命令时,该命令将成功完成,因此我不确定在应用相同转义的情况下调用Excel时,为什么这在Excel中不起作用

"C:\Program Files\myprogram.exe" -s:"D:\outputfiles\out.xls" -xsl:"C:\Program Files\stylesheet.xsl" -o:"D:\outputfiles\out.csv" > "D:\outputfiles\out_errors.log" 2>&1

感谢您的任何建议或帮助。

请将此用于您的
cmd1
线路

cmd1 = """""" & firstFilePath & """ -s:""" & strFilePath & """ -xsl:""" & secondFilePath & """ -o:""" & outfile & ".csv"" > """ & logfile & """ 2>&1"""

双引号<当需要
在字符串路径中。谢谢,我也尝试过,但在
firstFilePath
secondFilePath
字符串周围添加双引号时,似乎不起作用?对我来说似乎不起作用,出于某些原因,这也不会写出日志文件。您可能还需要将整个命令用引号括起来-请尝试使用我的更新(刚刚编辑过的)dude,这太棒了,现在工作得很好:)非常感谢您的快速回复。