在 VBA 中使用 'wsh.Run' 并在文件路径中使用空格
Using 'wsh.Run' in VBA with spaces in filepath
我在使用 WSH.run 命令和转义文件路径中的空格时遇到问题 - 它在没有空格的情况下工作正常!我尝试在每个 parameter/filepath 周围使用双引号、三引号和四引号,但是当从 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 中调用它时它不起作用是否应用了相同的转义?
"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"""
我在使用 WSH.run 命令和转义文件路径中的空格时遇到问题 - 它在没有空格的情况下工作正常!我尝试在每个 parameter/filepath 周围使用双引号、三引号和四引号,但是当从 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 中调用它时它不起作用是否应用了相同的转义?
"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"""