Calling Python Script from VBA, and waiting for script to finish

SkywardPalm

Board Regular
Joined
Oct 23, 2021
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I am attempting to call a python script from VBA and wait for the script to finish before continuing with the macro. However, It doesn't seem to be calling the python script.. any ideas what I may be doing wrong?

Code I have so far:
VBA Code:
Dim objShell As Object
Dim PythonExe, PythonScript As String

Set objShell = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0

PythonExe = """C:\Python\Python39\python.exe"""
PythonScript = "C:\Users\Me\PycharmProjects\Automation\Matching.py"

objShell.Run "cmd.exe /S /C" & PythonExe & PythonScript, vbHide, windowStyle, waitOnReturn
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
It could be a spacing issue: both after the /C and between PythonExe and PythonScript.

Also, you have 4 parameters: I'm not sure that vbHide is what you want since you're also using windowStyle. Either one should work because vbHide is 0, but you have too many arguments and waitOnReturn should be the third.

VBA Code:
objShell.Run "cmd.exe /s /c " & PythonExe & " " & PythonScript, windowStyle, waitOnReturn
 
Upvote 0
Solution
It could be a spacing issue: both after the /C and between PythonExe and PythonScript.

Also, you have 4 parameters: I'm not sure that vbHide is what you want since you're also using windowStyle. Either one should work because vbHide is 0, but you have too many arguments and waitOnReturn should be the third.

VBA Code:
objShell.Run "cmd.exe /s /c " & PythonExe & " " & PythonScript, windowStyle, waitOnReturn
I'm attempting to try out the solution you provided, however I'm stuck in a catch-22 where my python script won't write to an open excel file, but I need the excel file open to run the macro.. Here is a link to the problem I am having if you wouldn't mind taking a look (assuming you know python)
 
Upvote 0
I'm attempting to try out the solution you provided, however I'm stuck in a catch-22 where my python script won't write to an open excel file, but I need the excel file open to run the macro.. Here is a link to the problem I am having if you wouldn't mind taking a look (assuming you know python)
How about having two separate Excel files:
- one containing your VBA code
- one is the workbook you want to write the data to
 
Upvote 0
This seemed to work for me! Thanks for the advice
VBA Code:
'
' Run Python Script
'
    Set objShell = VBA.CreateObject("WScript.Shell")
    PythonExe = """C:\Python\Python39\python.exe"""
    PythonScript = "C:\Python\PycharmProjects\Automation\AutomationMatching.py"
    objShell.Run PythonExe & " " & PythonScript, windowStyle, waitOnReturn
 
Upvote 0
How about having two separate Excel files:
- one containing your VBA code
- one is the workbook you want to write the data to
I solved this issue I was having, I was trying to write to an open excel file with Openpyxl, which does not allow this. When I swapped to xlwings it ran smoothly and wrote to the open excel sheet. Thanks for the suggestion though, I was avoiding the extra work of opening and closing files (or writing to multiple books)
 
Upvote 0
I am not familiar with "Openpyxl", but glad you got it figured out.
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,123,000
Members
449,092
Latest member
masterms

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