I am trying to run a python script in excel VBA like this:
Shell("python " & ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12""")
but it does nothing. If I watch the task manager, a python process doesn’t even get created. If I debug and copy the string created in the code above and paste it into command prompt it runs perfectly.
The python script takes a date (as a string) as a parameter and creates an image file.
Any idea why this would not work or how I could debug it?
I’ve also tried this:
Shell ("python CreateVolsurface.py -d ""10 Sep 12""")
and this:
ret_val = Shell("python " & ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12""", vbHide)
but they also do nothing.
The issue was that shell() in vba starts cmd.exe in it’s default folder and not in the active folder of the current workbook. In my python script I had relative path references which was an issue. To solve this I used the os module in my python script to change the current directory at the start.
Try specifying the path to python.exe, or you may need to add the path to your %PATH% environment variable.
Try the following to verify Shell is working and step through with the debugger:
If that works, then you should try specifying the path:
If you need to check if python is in your %PATH% environment variable, the Process Explorer (procexp.exe) utility from sysinternals can show you all of the environment settings for a running process.