9

I am trying to call a Word macro using Python. I tried to adopt the logic used to run Excel VBA Macros through Python (there are plenty of examples of how to run Excel Macros through python, but I did not find any for word). Here is the code I am trying to use. However, what I get is that Python keeps running forever and I don't know why. What am I doing wrong?

Here is my attempt:

import os
import comtypes.client

path=r"path to my folder containing file.docx"

word=comtypes.client.CreateObject("Word.Application")
docx=word.Documents.Open(path, ReadOnly=1)
docx=word.Application.Run("My_Macro_Name")
word.Documents(1).Close(SaveChanges=0)
word.Application.Quit()
wd=0

Also I have tried to specify better the position of my VBA Macro (I saved it in a .docm file), but still there is no result. Python keeps running without showing any result. Here is my attempt:

import os
import comtypes.client

path=r"path to the .docm file where I saved the vba macro"

word=comtypes.client.CreateObject("Word.Application")
word.Documents.Open(path,ReadOnly=1)
word.Run("Project.Modulo1.ConvertDoc")
word.Documents(1).Close(SaveChanges=0)
word.Application.Quit()
wd=0
4
  • 1
    See the examples here: learn.microsoft.com/en-us/office/vba/api/word.application.run Try being more explicit about the location of the macro you want to run. Commented Jul 16, 2019 at 21:03
  • 2
    Try word.Run("ModuleName.ProcedureName"), word is already a Word.Application object, so word.Application should be redundant. Why reassign docx? Commented Jul 16, 2019 at 21:06
  • 2
    @AriYxm, Shouldn't it be something of the form: word.Run("'My Document.doc'!ThisModule.ThisProcedure") Commented Jul 17, 2019 at 0:17
  • Thank you all! Indeed I had to save the document with the macro as a .docm. Also I had misunderstood how to make correct reference to the name of the macro. Now everything works. Commented Jul 17, 2019 at 2:06

1 Answer 1

1

Why don't you run your VBA marco indirectly via Cscript/Wscript? You don't have to use comtypes package. Let's create a VBS file to run marco:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("test.xls")

objExcel.Application.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"

objExcel.Application.Run "Macro.TestMacro()"
objExcel.ActiveWorkbook.Close


objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit

BAT file:

C:\Windows\System32\cscript.exe D:\Script.vbs > output.log
exit

And now you can run a batch file from Python:

import subprocess
subprocess.call([r'path where the batch file is stored\name of the batch file.bat'])

You can read its response in output log file.

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

Comments

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.