Dealing with space in file path in VBA

ぐ巨炮叔叔 提交于 2019-12-11 05:56:34

问题


I have the below VBA code that I'm trying to run in cmd. There is a space in the path and I'm using quotes to deal with that.

strWot2Shell = """C:\Program Files\gs\gs9.20\bin\gswin64.exe""" & " -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile=" & Chr(34) & ripDirectory & "\LagerRiskRapport%01d.png" & Chr(34) & " " & Chr(34) & filePathAndFileName & Chr(34)
strWot2Shell = Replace(strWot2Shell, "\", "/")
strWot2Shell2 = "cmd.exe /S /C " & strWot2Shell
wsh.Run strWot2Shell2, windowStyle, waitOnReturn

The output from the code as below:

cmd.exe /S /C "C:/Program Files/gs/gs9.20/bin/gswin64.exe" -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile="G:/Rip/LagerRiskRapport%01d.png" "G:/LagerRiskRapport.pdf"

When I run this direclty in cmd I get:

'C:/Program' is not recognized as an internal or external command,
operable program or batch file.

If I run below directly in cmd it works

"C:/Program Files/gs/gs9.20/bin/gswin64.exe" -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile="G:/Rip/LagerRiskRapport%01d.png" "G:/LagerRiskRapport.pdf"

Triple quoting all string like this:

strWot2Shell = """C:\Program Files\gs\gs9.20\bin\gswin64.exe""" & """ -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile=""" & Chr(34) & ripDirectory & """\LagerRiskRapport%01d.png""" & Chr(34) & " " & Chr(34) & filePathAndFileName & Chr(34)

Gives this output:

cmd.exe /S /C "C:/Program Files/gs/gs9.20/bin/gswin64.exe"" -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile=""G:/Rip"/LagerRiskRapport%01d.png"" "G:/LagerRiskRapport.pdf"

Surrounding cmd.exe /S /C in quotes like"cmd.exe /S /C" results in The system cannot find the path specified.

Why are the quotes not working?


回答1:


This may be because you are triple quoting at the beginning and then not at the end. So try changing this:

"""C:\Program Files\gs\gs9.20\bin\gswin64.exe""" & " -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile=" & Chr(34) & ripDirectory & "\LagerRiskRapport%01d.png" & Chr(34) & " " & Chr(34) & filePathAndFileName & Chr(34)

To:

"""C:\Program Files\gs\gs9.20\bin\gswin64.exe""" & "" -dBATCH -dNOPAUSE -sDEVICE=png16m -r600 -sOutputFile="" & Chr(34) & ripDirectory & ""\LagerRiskRapport%01d.png"" & Chr(34) & " " & Chr(34) & filePathAndFileName & Chr(34)


来源:https://stackoverflow.com/questions/41933387/dealing-with-space-in-file-path-in-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!