Question

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
Was it helpful?

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

OTHER TIPS

The OpenNoRepairDialog method does not appear to have any error suppression options, as per;

http://msdn.microsoft.com/en-us/library/office/ee426710(v=office.12).aspx

It's possible you could disable these errors within Word preference somewhere.

Alternatively, try using pywin32?

http://www.galalaly.me/index.php/2011/09/use-python-to-parse-microsoft-word-documents-using-pywin32-library/

These might also be relevant;

Reading/Writing MS Word files in Python

https://github.com/mikemaccana/python-docx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top