1

I want to develop a tool where it is necessary to start a Python script via VBA.

I found the following code on the internet:

Sub RunPythonScript() '''This sub starts the Python script that creates the plots

    Dim objShell As Object
    ActiveWorkbook.Save
    Dim PythonExePath, PythonScriptPath As String

    Set objShell = VBA.CreateObject("Wscript.Shell")
    
    'PythonExePath = """C:\Users\MyName\AppData\Local\Programs\Python\Python38-32\python.exe""" ' 
    PythonScriptPath = "C:\Projects\MYproject\cttdb_plot_creator.py" 
    
    objShell.Run PythonExePath & PythonScriptPath

End Sub

This code works for me. However, when a colleague starts the tool from his computer, it does not work.

I think that I have entered too specific a path for "PythonExePath", namely: C:\Users\MyName\AppData\Local\Programs\Python\Python38-32\python.exe.

Is there a way to enter a path that is more general and works for every user?

2
  • Look up the Where command for windows command line. Commented Mar 15, 2021 at 16:48
  • Hey, freeflow. Thank you for your comment. But i'm new in coding.....what do you mean with your answer? Commented Mar 15, 2021 at 16:51

1 Answer 1

1

Simply have all users include the python.exe installation folder in their Path environment variable. On Windows which may require admin access, follow:

Control Panel > System and Security > System > Advanced system settings 
 > Environment Variables... > Path > OK > [Add new folder where python.exe resides]

See screenshot dialogs:

Windows Environment Path Dialogs

Verify with PowerShell ($Env:Path) or CMD Prompt (echo %Path%).

Once done, you can shorten your command line with:

PythonExePath = "python"
PythonScriptPath = "C:\Projects\MYproject\cttdb_plot_creator.py" 
    
objShell.Run PythonExePath & " " & PythonScriptPath
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. This was the solution. I will mark as correct answer.
Great to hear and glad to help! I hope screenshots help future readers as simply saying add python to environment Path variable may be formidable for newcomers.

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.