excel 如何以高优先级运行脚本?

nhaq1z21  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(96)

我想从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启动它已经与高优先级-在这方面,我没有找到答案,不幸的是,虽然花了几个小时。

mm5n2pyu

mm5n2pyu1#

这对我很有效(添加了/k以在测试时查看输出):

Sub Tester()

    Dim newShell As Object
    Dim PythonExePath As String, PythonScriptPath As String
    
    Set newShell = VBA.CreateObject("Wscript.Shell")
    
    PythonExePath = "C:\Python\Python39-32\python.exe"
    PythonScriptPath = "C:\Temp\VBA\test.py"
    
    newShell.Run "cmd.exe start /high /k """"" & PythonExePath & _
                 """ """ & PythonScriptPath & """""", vbNormalFocus
    
End Sub

相关问题