Word Automation - 禁用警报
我正在自动化 Word,因此我想抑制所有警报。
Word.Application word = new Word.Application();
word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
我使用上面列出的代码,但我不断从 Word 收到消息 - 例如,文档已打开。有办法避免这种情况吗?如何抑制所有这些愚蠢的消息框?
编辑:
考虑到所有答案,我想看门狗确实是唯一的选择。不幸的是,过去几年没有任何改变。
如果没有真正的答案,线程可以关闭。
I am automating Word, so I would like to suppress all alerts.
Word.Application word = new Word.Application();
word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
I use the code listed above, but I keep getting messages from Word - e.g., the document is already open. Is there a way to avoid this? How to supress all those stupid messageboxes?
EDIT:
Concider to all answers, I guess a watchdog is really the only option. To bad nothing has changed in last years.
Thread can be closed without a real answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码部分显然看起来是正确的,但不足以让我重现您所描述的行为。但是...
我强烈怀疑答案是您无法完全抑制 Word 中的消息。它被设计为用户交互式应用程序,并且
DisplayAlerts
设置有点碰运气。某些错误看起来可能不受指定警报设置的影响,但无论如何都会显示。例如,我以前见过拼写和语法错误的行为像这样。最好的办法是尝试预测可能发生的错误并避免生成错误。就您的情况而言,您应该检查要打开的文档是否已经打开。我不确定为什么您首先会生成此错误(您是否捕获错误并然后搜索文档的现有实例以对其进行操作,或者您的代码继续运行?),但在开始执行相关代码之前插入一些保护子句应该相当简单。
The portion of code that you have posted obviously looks correct, but there isn't enough for me to reproduce the behavior that you describe. However...
I have a strong suspicion the answer is that you can't completely suppress messages in Word. It was designed as a user-interactive application, and the
DisplayAlerts
setting is a bit of a hit-or-miss proposition. Certain errors look like they might be exempted from the specified alert setting and be displayed anyway. For example, I've seen spelling and grammar errors behave like this before.The best thing to do is try to anticipate the errors that may occur and avoid generating them. In your case, you should be checking to see if the document you're about to open is already open. I'm not sure why you'd ever be generating this error in the first place (are you catching the error and then searching for the existing instance of the document to operate on it, or how does your code continue running?), but it should be fairly straightforward to insert a few guard clauses before you start executing the relevant code.