【发布时间】:2021-07-23 19:15:36
【问题描述】:
我试图在 Excel 中的 VBA 宏中调用 powershell 脚本,但由于文件路径中的空格而遇到错误。我看到过类似的问题,人们建议在哪里用双引号、Chr(34) 等将路径括起来,但这没有用。
这是我的代码:
Path = ActiveWorkbook.Path
shell_path = Path & "\upload.ps1"
shell_script = "POWERSHELL.exe -noexit """ & shell_path & """"
Shell (shell_script)
变量值:
- Path = "Y:\文件路径"
- shell_path = "Y:\文件路径\upload.ps1"
- shell_script = "POWERSHELL.exe -noexit "Y:\File Path\upload.ps1""
在运行 Shell(shell_script) 时,我在 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
Shell 命令似乎忽略了 shell_script 字符串中文件路径周围的额外引号。此外,如果我将文件移动到没有空格的路径,一切都会运行良好(但这不是长期解决方案)。有什么想法吗?
【问题讨论】:
标签: vba powershell