Word Automation - 禁用警报

发布于 2024-10-04 02:56:40 字数 354 浏览 0 评论 0原文

我正在自动化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2024-10-11 02:56:40

您发布的代码部分显然看起来是正确的,但不足以让我重现您所描述的行为。但是...

我强烈怀疑答案是您无法完全抑制 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文