PowerShell 命令在从 VBA 调用时不起作用,但在其他方面起作用
PowerShell command doesn't work when called from VBA, but otherwise works
目前我无法使用 xlwings,因为我无法访问 Windows' cmd。但是我可以访问 PowerShell,所以我尝试更改调用 cmd 的 xlwings 特定 VBA 代码来调用 PowerShell。
当使用来自 VBA 的命令调用 PowerShell 时,它不起作用。如果我在 PowerShell 终端中粘贴完全相同的命令,它会按预期工作。
我尝试将传递给 PowerShell 的(无效)命令 VBA 与我手动粘贴到 PowerShell 终端的(有效)命令进行比较。但它们看起来完全一样。
调用cmd的原xlwings代码
RunCommand = PythonInterpreter & " -B -c ""import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'" & PYTHONPATH & "')).split(';'); " & PythonCommand & """ "
ExitCode = Wsh.Run("cmd.exe /C " & _
DriveCommand & RunCommand & _
"""" & WORKBOOK_FULLNAME & """ ""from_xl""" & " " & _
Chr(34) & Application.Path & "\" & Application.Name & Chr(34) & " " & _
Chr(34) & Application.Hwnd & Chr(34) & _
" 2> """ & LOG_FILE & """ ", _
WindowStyle, WaitOnReturn)
还有我稍微修改过的版本
RunCommand = "C:\ProgramData\Anaconda3\pythonw.exe -B -c ""import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'" & PYTHONPATH & "')).split(';'); " & PythonCommand & """ "
ExitCode = Wsh.Run("Powershell.exe -ExecutionPolicy ByPass -NoExit " & _
DriveCommand & RunCommand & _
"""" & WORKBOOK_FULLNAME & """ ""from_xl""" & " " & _
Chr(34) & Application.Path & "\" & Application.Name & Chr(34) & " " & _
Chr(34) & Application.Hwnd & Chr(34) & _
" 2> """ & LOG_FILE & """ ", _
WindowStyle, WaitOnReturn)
上述代码的结果命令。直接粘贴到 PowerShells 终端时工作,从 VBA:
执行时不工作
C:\ProgramData\Anaconda3\pythonw.exe -B -c "import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'C:\Users\<placeholder>\test1;')).split(';'); import test1;test1.hello_xlwings()" "C:\Users\<placeholder>\test1\test1.xlsm" "from_xl" "C:\Program Files (x86)\Microsoft Office\Office16\Microsoft Excel" "788640" 2> "C:\Users\<placeholder>\AppData\Roaming\xlwings.log"
当单击与 vba 宏关联的按钮时,我希望在特定 Excel 单元格中出现一个简单的 "Hello, World!"。相反,我收到此错误:
At line:1 char:213
+ ... X0RNZ\test1;')).split(';'); import test1;test1.hello_xlwings() C:\Use ...
+ ~
An expression was expected after "(".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
如果我直接将命令粘贴到 PowerShell 终端中,我会得到预期的结果,"Hello, World!" 会出现在我的特定 Excel 单元格中。
您缺少 -Command
参数。根据 DriveCommand
包含的内容,您应该在 DriveCommand
或 RunCommand
.
之前添加 -Command
确保de PowerShell命令之间有一个分号并将命令指定为脚本块,例如:
powershell.exe -ExecutionPolicy ByPass -NoExit -Command { cd "c:\folder";c:\folder\run.exe "param1" "param2" }
运行 powershell /?
更多示例。
目前我无法使用 xlwings,因为我无法访问 Windows' cmd。但是我可以访问 PowerShell,所以我尝试更改调用 cmd 的 xlwings 特定 VBA 代码来调用 PowerShell。
当使用来自 VBA 的命令调用 PowerShell 时,它不起作用。如果我在 PowerShell 终端中粘贴完全相同的命令,它会按预期工作。
我尝试将传递给 PowerShell 的(无效)命令 VBA 与我手动粘贴到 PowerShell 终端的(有效)命令进行比较。但它们看起来完全一样。
调用cmd的原xlwings代码
RunCommand = PythonInterpreter & " -B -c ""import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'" & PYTHONPATH & "')).split(';'); " & PythonCommand & """ "
ExitCode = Wsh.Run("cmd.exe /C " & _
DriveCommand & RunCommand & _
"""" & WORKBOOK_FULLNAME & """ ""from_xl""" & " " & _
Chr(34) & Application.Path & "\" & Application.Name & Chr(34) & " " & _
Chr(34) & Application.Hwnd & Chr(34) & _
" 2> """ & LOG_FILE & """ ", _
WindowStyle, WaitOnReturn)
还有我稍微修改过的版本
RunCommand = "C:\ProgramData\Anaconda3\pythonw.exe -B -c ""import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'" & PYTHONPATH & "')).split(';'); " & PythonCommand & """ "
ExitCode = Wsh.Run("Powershell.exe -ExecutionPolicy ByPass -NoExit " & _
DriveCommand & RunCommand & _
"""" & WORKBOOK_FULLNAME & """ ""from_xl""" & " " & _
Chr(34) & Application.Path & "\" & Application.Name & Chr(34) & " " & _
Chr(34) & Application.Hwnd & Chr(34) & _
" 2> """ & LOG_FILE & """ ", _
WindowStyle, WaitOnReturn)
上述代码的结果命令。直接粘贴到 PowerShells 终端时工作,从 VBA:
执行时不工作C:\ProgramData\Anaconda3\pythonw.exe -B -c "import sys, os; sys.path[0:0]=os.path.normcase(os.path.expandvars(r'C:\Users\<placeholder>\test1;')).split(';'); import test1;test1.hello_xlwings()" "C:\Users\<placeholder>\test1\test1.xlsm" "from_xl" "C:\Program Files (x86)\Microsoft Office\Office16\Microsoft Excel" "788640" 2> "C:\Users\<placeholder>\AppData\Roaming\xlwings.log"
当单击与 vba 宏关联的按钮时,我希望在特定 Excel 单元格中出现一个简单的 "Hello, World!"。相反,我收到此错误:
At line:1 char:213 + ... X0RNZ\test1;')).split(';'); import test1;test1.hello_xlwings() C:\Use ... + ~ An expression was expected after "(". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression
如果我直接将命令粘贴到 PowerShell 终端中,我会得到预期的结果,"Hello, World!" 会出现在我的特定 Excel 单元格中。
您缺少 -Command
参数。根据 DriveCommand
包含的内容,您应该在 DriveCommand
或 RunCommand
.
-Command
确保de PowerShell命令之间有一个分号并将命令指定为脚本块,例如:
powershell.exe -ExecutionPolicy ByPass -NoExit -Command { cd "c:\folder";c:\folder\run.exe "param1" "param2" }
运行 powershell /?
更多示例。