3

I have trouble running a python script from Excel VBA. This is the VBA code:

Sub RunPython()
    
    Dim objShell As Object
    Dim PythonExe, PythonScript As String
    Set objShell = VBA.CreateObject("Wscript.Shell")
    PythonExe = """C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe""" 
    PythonScript = """C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"""  
    objShell.Run PythonExe & PythonScript

End Sub

The python code is the following.

print("hello")
input("hello, press enter")

When I run VBA, the python executer opens but it immediately closes without actually executing the script. In other words, neither "hello" nor "hello, press enter" appear on the python executer window, which suddenly closes.

I'm sure the paths to the python.exe and to the python script are correct. I've tried them both with """" and with "" and in both cases it does not work. Obviously (it's super simple!) the python script works if I run it directly from the python executer.

0

2 Answers 2

2

SOLVED!! I can't understand why, but splitting objShell.Run PythonExe and objShell.Run PythonScript it works fine! Like that:

Public Sub RunPython()    
    Dim objShell As Object
    Set objShell = VBA.CreateObject("Wscript.Shell")

    Dim PythonExe As String
    PythonExe = """C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe""" 

    Dim PythonScript As String
    PythonScript = """C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"""  
    
     objShell.Run PythonExe
     objShell.Run PythonScript

End Sub

Thanks anyway @Pᴇʜ , I'll rate your answer as positive 'cause you have been very kind!!

Sign up to request clarification or add additional context in comments.

2 Comments

Just check if it works without objShell.Run PythonExe. And of course you can accept your own answer as solution (after 1 day I think).
Then that means it works because windows linked the .py file type with the python automatically.
0

You probably need a space between PythonExe & " " & PythonScript. So the command you run is "C:\…\python3.exe" "C:\…\BlackBoxAbsorbers.py" with space between the program path and the python script.

Also note that Dim PythonExe, PythonScript As String only declares PythonScript As String but PythonExe As Variant. In VBA you need to specify a type for every variable: Dim PythonExe As String, PythonScript As String or it is Variant by default!

Further objShell.Run PythonExe & PythonScript is not properly debugable. I recommend to put the command into a variable so you are able to print it in the immediate window for debugging:

Dim Command As String
Command = PythonExe & PythonScript

Debug.Print Command

objShell.Run Command 

If you do this you will see that the output of your command is:

"C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe""C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"

And there is no space between the 2 strings, where it actually should be:

"C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe" "C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"

So I recommend:

Public Sub RunPython()    
    Dim objShell As Object
    Set objShell = VBA.CreateObject("Wscript.Shell")

    Dim PythonExe As String
    PythonExe = """C:\Users\carlo\AppData\Local\Microsoft\WindowsApps\python3.exe""" 

    Dim PythonScript As String
    PythonScript = """C:\Users\carlo\Desktop\MSc_Thesis\ML applications\BlackBox\BlackBoxAbsorbers.py"""  

    Dim Command As String
    Command = PythonExe & " " & PythonScript

    objShell.Run Command
End Sub

2 Comments

Thanks for answering me! But unfortunately, even your version of the code does not work for me.. :((
@CarloCortellini does Shell Command work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.