Execute Python code within VBA

jhoward0101

New Member
Joined
Jan 7, 2017
Messages
15
Hello. I am hoping to get some help to execute python code via VBA rather than executing a command batch file. I believe the reason the .py file is not executing because pathing in incorrect.
Here is the batch file that works:
C:
cd C:\users\Jim Howard\Desktop\TDDevelopment
path = %path%;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\TDApp;C:\Users\anaconda;C:\Users\anaconda\scripts;C:\Users\anaconda\Library\mingw-w64\bin;C:\Users\anaconda\Library\usr\bin;C:\Users\anaconda\Library\bin;C:\Users\anaconda\bin;C:\Users\anaconda\condabin;C:\Users\anaconda\Lib\site-packages\future\moves
erase status.txt
erase query.txt
python ordersquery.py
exit

and here is a short VBA module I am trying to use:

Sub RunPython()
Dim shell As Object
Dim exePath, scriptPath As String

Set shell = VBA.CreateObject("Wscript.shell")

spath = Environ$("systemroot\System32\" & ";" & "systemroot\System32\Wbem" & ";" & "systemroot\System32\WindowsPowerShell\v1.0\" & ";C:\TDApp;C:\Users\anaconda;C:\Users\anaconda\scripts;C:\Users\anaconda\Library\mingw-w64\bin;C:\Users\anaconda\Library\usr\bin;C:\Users\anaconda\Library\bin;C:\Users\anaconda\bin;C:\Users\anaconda\condabin;C:\Users\anaconda\Lib\site-packages\future\moves")

exePath = """C:\Users\Jim Howard\python.exe"""

scriptPath = "C:\Desktop\Tddevelopment\ordersquery.py"

shell.Run exePath & scriptPath

End Sub

I left the commented spath statement in to see what I have tried but it does not execute the .py flle, it also does not fail with an error, so I think if i could get the pathing right, it would work. I think python is being executed from anaconda with the batch file.

Any help would be much appreciated.

Thanks in advance,
James Howard
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about this.
VBA Code:
Sub RunPython()
    Dim exePath As String, scriptPath As String, ShellToPythonCmd As String

    exePath = "C:\Users\Jim Howard\python.exe"
    scriptPath = "C:\Desktop\Tddevelopment\ordersquery.py"
    ShellToPythonCmd = exePath & " " & scriptPath

    With VBA.CreateObject("Wscript.shell")
        .Run ShellToPythonCmd
    End With
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above

How to Post Your VBA Code

as it makes the code easier to read.)
 
Upvote 0
How about this.
VBA Code:
Sub RunPython()
    Dim exePath As String, scriptPath As String, ShellToPythonCmd As String

    exePath = "C:\Users\Jim Howard\python.exe"
    scriptPath = "C:\Desktop\Tddevelopment\ordersquery.py"
    ShellToPythonCmd = exePath & " " & scriptPath

    With VBA.CreateObject("Wscript.shell")
        .Run ShellToPythonCmd
    End With
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above

How to Post Your VBA Code

as it makes the code easier to read.)
Thanks for your reply rlv01.
When running the module I receive a Run Time error -'2147024898(80070002)': "Method of 'Run' of object 'IWshShell3' failed.
 
Upvote 0
The most likely explanation is that your exePath is not correct. In other words

VBA Code:
    exePath = "C:\Users\Jim Howard\python.exe"

There is no file python.exe in that folder (C:\Users\Jim Howard\)
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top