我想从Excel VBA运行我的python脚本和这段代码的工作:
Dim newShell As Object
Dim PythonExePath As String, PythonScriptPath As String
Set newShell = VBA.CreateObject("Wscript.Shell")
PythonExePath = """C:\MyPythonPath\pythonw.exe"""
PythonScriptPath = """C:\MyScriptPath\anyscript.py"""
newShell.Run PythonExePath & PythonScriptPath`
我想以高优先级运行它,所以我在cmd.exe中尝试了下一个构造:start /high "C:\MyPythonPath\pythonw.exe" "C:\MyScriptPath\anyscript.py"
,它工作得更快一点。所以我想使用相同的结构,当从Excel VBA调用时,但我找不到一种方法,我应该如何插入“开始/高”部分。我尝试了许多变化,但它仍然给我一个错误“运行时错误”-2147024894(80070002):方法“Run' of object ' IWshShell 3 ' failed”,据我所知,它指出它找不到文件。
所以,请告诉我如何正确地调用它。例如,在不起作用的变体中有以下几个(特别是如果你想取笑我的无知):
newShell.Run "start " & Chr(34) & Chr(34) & " /high " & PythonExePath & PythonScriptPath`
newShell.Run "start " & Chr(34) & Chr(34) & " /high " & PythonExePath & " " & PythonScriptPath
newShell.Run "/high " & PythonExePath & " " & PythonScriptPath
newShell.Run "start /high ""C:\MyPythonPath\pythonw.exe"" ""C:\MyScriptPath\anyscript.py"""
newShell.Exec ("start /high " & PythonExePath & " " & PythonScriptPath)
newShell.Exec ("start /high ""C:\MyPythonPath\pythonw.exe"" ""C:\MyScriptPath\anyscript.py""")
还有更多我已经找到了一些解决方案,使运行过程的优先级更高,从内部的python代码,但我想从VBA启动它已经与高优先级-在这方面,我没有找到答案,不幸的是,虽然花了几个小时。
1条答案
按热度按时间mm5n2pyu1#
这对我很有效(添加了
/k
以在测试时查看输出):