2

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.

3
  • Hi, I took a different approach, I don't shell, I use a "gateway class". So called gateway because it opens the rich world of the Python ecosystem to the Excel VBA Developer exceldevelopmentplatform.blogspot.com/2018/06/… Commented Jun 11, 2018 at 20:46
  • @smeaden this solution is six years old so I'm sure there are a lot of great new options. I couldn't really follow what you mean by gateway class from your link. Can you explain it a bit further please? Commented Jun 11, 2018 at 20:56
  • Gateway class = a COM class. Commented Jun 11, 2018 at 21:09

1 Answer 1

4

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:

ret_val = Shell("notepad.exe", vbNormalFocus)

If that works, then you should try specifying the path:

args = ActiveWorkbook.Path & "\CreateVolsurface.py -d ""10 Sep 12"""
ret_val = Shell("C:\python27\python.exe " & args)

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.

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

1 Comment

Thanks, actually looks like the error was elsewhere but using normal focus helped me find it. Shell("") is launching cmd.exe BUT, and I guess this makes sense, it starts it in C:\Users...\Documents and not in the ActiveWorkbook.Path. I'm sure I'll figure out how to change that. Oh and the error was because I was reading a file using relative path name in the python script so it couldn't find it as was running from wrong directory :/

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.