简体   繁体   中英

Call Shell in VBA with spaces in the file path

I'm attempting to call a powershell script in an VBA macro in Excel, but run into an error due to spaces in the file path. I've seen similar questions asked where people suggest surrounding the path with double quotes, Chr(34), etc., but that hasn't worked.

Here is my code:

Path = ActiveWorkbook.Path
shell_path = Path & "\upload.ps1"
shell_script = "POWERSHELL.exe -noexit """ & shell_path & """"
Shell (shell_script)

Variable values:

  • Path = "Y:\\File Path"
  • shell_path = "Y:\\File Path\\upload.ps1"
  • shell_script = "POWERSHELL.exe -noexit "Y:\\File Path\\upload.ps1""

Upon running Shell(shell_script), I get the following error message in powershell:

Y:\File: The term 'Y:\File' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Y:\File Path\upload.ps1
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Y:\File:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

It seems like the Shell command is ignoring the extra set of quotes around the file path in the shell_script string. Also, if I move the file to a path that doesn't have spaces, everything runs fine (but that isn't a long term solution). Any ideas on what to do?

Based on You can escape the space by using single quotations and a backtick before the space :

Shell "POWERSHELL.exe -noexit " & Replace(ActiveWorkbook.Path, " ", "` ") & "\upload.ps1"

Addition

Also you can use FSO's ShortPath() which returns short path without spaces:

Sub test2()
    short_path = CreateObject("Scripting.FileSystemObject"). _
                 Getfile(ActiveWorkbook.Path & "\upload.ps1"). _
                 ShortPath  ' smth like "C:\test\FILEPA~1\upload.ps1"
    Shell "POWERSHELL.exe -noexit " & short_path
End Sub

Use the -file parameter of the PowerShell CLI , which makes your embedded " quoting ( "" ) work as intended:

script_path = ActiveWorkbook.Path & "\upload.ps1"
' Note the use of -file
shell_script = "POWERSHELL.exe -noexit -file """ & script_path & """"
Shell shell_script, vbNormalFocus

Note: I've added argument vbNormalFocus to the Shell command in order to run the command in a normal foreground window for easier troubleshooting. Remove it again to run the window minimized - or perhaps change to vbMinmizedNoFocus so that the minimized window doesn't receive the focus.

Without -file , -command is implied in Windows PowerShell [1] , and -command subjects your argument to another round of interpretation, as PowerShell code , after the enclosing "..." have been stripped during command-line parsing.

In other words: The command in your question is equivalent to executing
Y:\\File Path\\upload.ps1 - no quotes - from inside a PowerShell session, which predictably fails, because the path isn't recognized as a whole, and only the Y:\\File part is interpreted as the executable.

See this answer for more information.


[1] By contrast, the cross-platform, install-on-demand PowerShell (Core) edition (v6+) now defaults to -file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM