简体   繁体   English

从VBA调用时,PowerShell命令不起作用,但其他方式有效

[英]PowerShell command doesn't work when called from VBA, but otherwise works

Currently I can't use xlwings because I can't access Windows' cmd. 目前我无法使用xlwings,因为我无法访问Windows的cmd。 But I can access PowerShell, so I tried to change xlwings specific VBA code that calls the cmd to call the PowerShell instead. 但我可以访问PowerShell,因此我尝试更改调用cmd的xlwings特定VBA代码来调用PowerShell。

When calling PowerShell with commands from VBA, it does not work. 使用VBA中的命令调用PowerShell时,它不起作用。 If I paste the exact same commands in the PowerShell terminal, it works as expected. 如果我在PowerShell终端中粘贴完全相同的命令,它将按预期工作。

I've tried to compare the (not working) command VBA passes to PowerShell with the (working) command I manually paste into PowerShells terminal. 我试图将(不工作)命令VBA传递给PowerShell与(工作)命令进行比较我手动粘贴到PowerShells终端。 But they look exactly the same. 但它们看起来完全一样。

The original xlwings code that calls the cmd 调用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)

And my slightly modified version 而我的略有修改版本

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)

The resulting command from aboves code. 从上面的代码生成的命令。 Working when pasted into PowerShells terminal directly, not working when executed from VBA: 直接粘贴到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"

I expect a simple "Hello, World!" 我期待一个简单的“Hello,World!” in a specific Excel cell when clicking a button associated with the vba macro. 单击与vba宏关联的按钮时,在特定的Excel单元格中。 Instead I get this error: 相反,我得到这个错误:

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

If I paste the command in the PowerShell terminal directly I get my expected result, "Hello, World!" 如果我直接在PowerShell终端中粘贴命令,我得到了我的预期结果,“Hello,World!” shows up in my specific Excel cell. 显示在我的特定Excel单元格中。

You are missing the -Command parameter. 您缺少-Command参数。 Depending on what DriveCommand contains, you should add -Command before DriveCommand or RunCommand . 根据DriveCommand包含的内容,您应该在DriveCommandRunCommand之前添加-Command

Make sure there is a semicolon between de PowerShell commands and specify the command as a scriptblock, example: 确保de PowerShell命令之间有分号,并将命令指定为scriptblock,例如:

powershell.exe -ExecutionPolicy ByPass -NoExit -Command { cd "c:\folder";c:\folder\run.exe "param1" "param2" }

Run powershell /? 运行powershell /? for more examples. 更多例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA Excel 命令在使用 Shell 调用时不起作用,但在直接复制粘贴到 cmd 时起作用 - VBA Excel command doesn't work when called with Shell, but works when copy-pasted to cmd directly 我的第二个子程序在第一个子程序中调用时不起作用,但如果单独启动则可以工作 - My second sub doesn't work when called in the first sub but works if started separately 从Powershell内执行Excel VBA宏,因为宏内的循环不起作用 - Execute excel vba macro from within powershell, for loop inside macro doesn't work 从 VBA 代码工作,代码没有变化,突然代码不起作用 - From VBA code works, to no changes in the code and suddenly code doesn't work VBA代码在2010年有效,而广告在2007年无效 - vba code works in 2010 ad doesn't work in 2007 VBA WorksheetFunction.Sum不适用于数组,但适用于范围? - VBA WorksheetFunction.Sum doesn't work with array but works with range? CountIf函数在VBA函数中无法正常工作,但在sub - CountIf function doesn't work properly in VBA function, but works in sub 测试时,VBA 代码有效,正常运行时,不 - When Testing, VBA code works, When run normally, It doesn't 单击按钮时,命令按钮中的VBA代码不起作用 - VBA code in command button doesn't work on button click VBA Excel:子例程可以工作,但是如果被调用,它将不再起作用 - VBA Excel : Subroutine work but if called, It no longer works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM