How to Run External Python Script From Word VBA
🔍 WiseChecker

How to Run External Python Script From Word VBA

You want to call a Python script directly from Word using VBA. This is useful when you need to process data, generate reports, or interact with external systems that VBA cannot handle alone. Word VBA can launch any external executable, including the Python interpreter, by using the Shell function or the Windows Script Host object. This article explains how to set up VBA code that runs an external Python script, passes arguments, and waits for the script to finish before continuing.

Key Takeaways: Running Python Scripts From Word VBA

  • Shell function with python.exe path: Launches the Python interpreter in a separate window.
  • WshShell.Run method with waitOnReturn:=True: Runs the script synchronously and waits for completion.
  • Passing arguments via command-line string: Sends file paths or parameters to the Python script.

How Word VBA Communicates With External Programs

Word VBA does not have a built-in Python integration. To run a Python script, VBA uses the Windows operating system to start a new process. The two primary methods are the VBA Shell function and the Windows Script Host object (WshShell). Both methods call the Python interpreter (python.exe) and supply the script file path as an argument.

The Shell function launches the process asynchronously by default. This means VBA continues executing the next line of code before the Python script finishes. If your Word macro depends on the script output, you must use a synchronous method such as WshShell.Run with the waitOnReturn parameter set to True.

You also need the full path to python.exe and the full path to your .py file. Python must be installed on the same machine. If Python is not in the system PATH, you must provide the complete executable path.

Methods to Run a Python Script From Word VBA

Method 1: Using the VBA Shell Function

The Shell function is the simplest way to start an external program. It returns a task ID but does not wait for the program to end. Use this method when you do not need to wait for the Python script to finish.

  1. Open the VBA Editor
    Press Alt+F11 in Word to open the Visual Basic for Applications editor.
  2. Insert a new module
    In the Project Explorer, right-click your document or Normal project and choose Insert > Module.
  3. Write the Shell call
    Paste this code into the module:
    Sub RunPythonShell()
    Dim pythonPath As String
    Dim scriptPath As String
    pythonPath = "C:\Python39\python.exe"
    scriptPath = "C:\MyScripts\process.py"
    Shell pythonPath & " " & scriptPath, vbNormalFocus
    End Sub
  4. Adjust paths for your system
    Replace the pythonPath and scriptPath with the actual locations on your computer. If Python is in your PATH, you can use just “python.exe” instead of the full path.
  5. Run the macro
    Press F5 while the cursor is inside the subroutine, or close the editor and run the macro from Word via View > Macros.

Method 2: Using WshShell.Run to Wait for Completion

When your Word macro must wait for the Python script to finish before proceeding, use the Windows Script Host object. This method gives you control over window style and synchronization.

  1. Open the VBA Editor
    Press Alt+F11.
  2. Insert a new module
    Right-click the project and choose Insert > Module.
  3. Write the WshShell.Run call
    Paste this code:
    Sub RunPythonWsh()
    Dim wsh As Object
    Set wsh = CreateObject("WScript.Shell")
    Dim pythonPath As String
    Dim scriptPath As String
    Dim cmd As String
    pythonPath = "C:\Python39\python.exe"
    scriptPath = "C:\MyScripts\process.py"
    cmd = pythonPath & " " & scriptPath
    wsh.Run cmd, 0, True
    Set wsh = Nothing
    End Sub
  4. Understand the parameters
    The second parameter (0) hides the command window. Use 1 to show a normal window. The third parameter (True) tells VBA to wait until the script exits.
  5. Run the macro
    Press F5 or run from Word’s Macros dialog.

Method 3: Passing Arguments to the Python Script

To send data such as a document path or a variable from Word to Python, append arguments to the command string. Python reads these arguments via sys.argv.

  1. Modify the WshShell.Run code
    Add extra arguments after the script path:
    Sub RunPythonWithArgs()
    Dim wsh As Object
    Set wsh = CreateObject("WScript.Shell")
    Dim pythonPath As String
    Dim scriptPath As String
    Dim docPath As String
    pythonPath = "C:\Python39\python.exe"
    scriptPath = "C:\MyScripts\process.py"
    docPath = ActiveDocument.FullName
    wsh.Run pythonPath & " " & scriptPath & " " & Chr(34) & docPath & Chr(34), 0, True
    Set wsh = Nothing
    End Sub
  2. Enclose paths with spaces in quotes
    The Chr(34) function adds double quotes around the document path to handle spaces in file names.
  3. Write the Python script to receive the argument
    In your .py file, use sys.argv[1] to access the first argument passed from VBA.

Common Issues When Running Python From Word VBA

Word Freezes or Hangs While Waiting for Python

When you use WshShell.Run with waitOnReturn:=True, Word becomes unresponsive until the Python script finishes. This is normal behavior. To avoid a frozen interface, consider running the script asynchronously with Shell and checking later for completion, or display a status message to the user before the wait begins.

Python Script Runs but Produces No Output

The most common cause is an incorrect path to python.exe or the .py file. Test the command in a Command Prompt window first. If the command works there but not from VBA, the issue is often a missing file extension association or a PATH environment variable that is not available to the Word process. Always use the full path to python.exe.

VBA Cannot Find the Python Executable

If Python is installed only for the current user, the system PATH may not include it. Find the exact location of python.exe by opening a Command Prompt and running where python. Use that full path in your VBA code. Alternatively, add Python to the system PATH environment variable and restart Word.

Python Script Fails Because of Missing Modules

The Python interpreter launched by VBA runs with the same environment as the user account running Word. If your script imports third-party libraries that are not installed in that environment, the script will fail. Install required packages using pip from a command line that runs under the same user account.

Shell Function vs WshShell.Run: Key Differences

Item Shell Function WshShell.Run Method
Wait for completion No Yes when waitOnReturn=True
Window style control Limited to vbNormalFocus, vbHide, etc Full numeric control (0=hide, 1=normal, etc)
Return value Task ID (integer) Error level (integer)
Requires reference None Requires CreateObject(“WScript.Shell”)
Best use case Fire-and-forget scripts Scripts whose output is needed before continuing

Word VBA can launch external Python scripts reliably using either the Shell function or the Windows Script Host object. The WshShell.Run method with waitOnReturn:=True gives you synchronous execution and better control over the command window. Always test your command in a Command Prompt before embedding it in VBA. For scripts that modify Word documents, consider passing the document path as an argument so Python can access the file directly.