0

I'm want to run my python script from Excel VBA and this code works:

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`

I want to run it with high priority, so I tried in cmd.exe the next construction: start /high "C:\MyPythonPath\pythonw.exe" "C:\MyScriptPath\anyscript.py" and it works a little faster. So I want to use the same construction, when calling from Excel VBA, but I can't find a way how I should insert that "start /high" part. I tried many variations, but it still gives me an error "Run-time error '-2147024894 (80070002): Method "Run' of object 'IWshShell3' failed", which, as I understand, states, that it can't find the file.

So, please, show me the way to call it correctly. Among not working variants are, for example, these (especially if you want to make some fun of my ignorancy):

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""")

And some more. I've found some solutions to make the priority of the running process higher from inside the python code, but I would like to start it from VBA already with high priority — and on that I didn't find answers unfortunately, although spent several hours on it.

2
  • TBH, your supposedly working start command is incorrect. The start command usually understands the first doublequoted string to be the title, so you should be using one, even if it is empty. start "" /high "C:\MyPythonPath\pythonw.exe" "C:\MyScriptPath\anyscript.py". Also the entire /c or /k argument string, should itself be doublequoted: C:\Windows\System32\cmd.exe /q /d /c "start "" /high "C:\MyPythonPath\pythonw.exe" "C:\MyScriptPath\anyscript.py"".
    – Compo
    Commented Jun 16, 2023 at 21:29
  • @Compo, thank you for your note; I tried both with "" after the start command and without it — and it didn't work, the first two not working examples are with "" after the start command.
    – Zhimolost
    Commented Jun 17, 2023 at 9:10

1 Answer 1

1

This worked for me (added the /k to see the output when testing):

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
3
  • that is an immense amount of double quotes! :) I also thought that there may be some action with double quotes, but five of them was beyond my scope. :) However, your code on my PC for some reason just started cmd and didn't do anything more. I'm pretty sure the answer is somewhere around the number of double quotes, as you stated, so thank you for the idea. Apart from that, I decided to make a .bat file, where wrote start "" /high "C:\MyPythonPath\pythonw.exe" "C:\MyScriptPath\anyscript.py" and in the VBA code used newShell.Run "C:\MyScriptPath\help1.bat", 0. And it works.
    – Zhimolost
    Commented Jun 17, 2023 at 9:09
  • My test.py just wrote a single line of output, but it did run. You wouldn’t see any output if your script doesn’t create any. Commented Jun 17, 2023 at 17:00
  • my script really doesn't create any output in the cmd, but normally it does some operations after returning from python to Excel. And that didn't happen when I used your line. Anyway, thank you for your help, I will definitely use your variant in the future.
    – Zhimolost
    Commented Jun 20, 2023 at 22:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.