在 Python/Win32 中打开文档时如何关闭 MS Word 显示的对话框 [英] How to dismiss a dialog box displayed by MS Word when openning document in Python/Win32
问题描述
我正在尝试编写一个脚本,该脚本将遍历文件夹中所有 ms word 文档的内容并收集一些信息.我使用 Python 2.7.3 和 Ms Office 2007我遇到的问题是,有时 MS Word 在打开文档时会出现警告/错误弹出窗口.错误消息的一个示例是Microsoft Visual Basic: Sub or Function not defined".另一个错误是文档中的语法错误太多.这些事件会暂停处理,直到您手动按下 OK 按钮.之后,脚本继续处理文档.一个有趣的注意是,当我在 word 中手动打开文档时,它不会产生未定义子或函数"错误.
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?
这是我用来打开word文件的代码:
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
推荐答案
好的,我在 MS KB 中找到了答案.它适用于 VB,但由于 pywin32 只是 API 的包装,因此同样适用于 Python 代码.这是 MS KB 中文章的链接:http://support.microsoft.com/kb/259971
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
由于链接已失效,这里有 KB 的存档:https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/259971
since the link is dead, there is an archive of the KB here: https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/259971
这篇关于在 Python/Win32 中打开文档时如何关闭 MS Word 显示的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!