Search code examples
c#.netms-wordoffice-interop

Word Automation - Disable Alerts


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.


Solution

  • 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.