Search code examples
pythonms-officepywin32

How to dismiss a dialog box displayed by MS Word when openning document in Python/Win32


I'm trying to write a script that would go though content of all ms word docs in a folder and collect some information. I use Python 2.7.3 and Ms Office 2007 The issue I have is that sometimes MS Word is coming with a warning/error popup window when opening a document. An example of the error message is 'Microsoft Visual Basic: Sub or Function not defined'. Another error is that too many grammar mistakes in the doc. These events suspend the processing until you manually press OK button. After that the scrip continues processing the document. An interesting note is that when I open the document manually in word it doesn't produce the 'Sub or Function not defined' error.

So, is there a way to suppress these errors/warnings so that the script could process the files in a batch mode?

Here is the code I use to open the word files:

import win32com.client
word = win32com.client.Dispatch("Word.Application")
word.Visible = False
...
doc = word.Documents.OpenNoRepairDialog(fname, False, True) # Popup window appears on this line

Solution

  • Ok, I have found the answer in the MS KB. It's for VB but as pywin32 is only a wrap around the API the same is applicable to the python code. Here is the link to the article in MS KB: http://support.microsoft.com/kb/259971

    EDIT: since the link is dead, there is an archive of the KB here: https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/259971