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
word.Run("ModuleName.ProcedureName"),wordis already aWord.Applicationobject, soword.Applicationshould be redundant. Why reassigndocx?word.Run("'My Document.doc'!ThisModule.ThisProcedure")