0

When a Word document is opened in my Word Add-in, I validate the document. The validation requires the document to be saved so I call the save method:

Microsoft.Office.Tools.Word.Document.Save()

If the document is read-only, (my use case is opening an attached document from Outlook), the Save As dialog opens. If I save the document or hit cancel, a COMException is thrown for both cases with the message "This file is read-only. (File name.)".

Typically, a user cancellation COMException is thrown for Cancel and I would expect no exception to be thrown if the file is saved correctly. Is this a bug or do I need to handle the file differently?

Joe W
  • 1,789
  • 3
  • 28
  • 42

1 Answers1

1

There are two things you could do to avoid described behaviour (at least works great for me). First, you should use save as -functionality for it: document.SaveAs("/MyDocuments/example.doc"). You can also "force" application to save the document without any dialogs and you can achieve this by useing Document.Application.DiplayAlerts = False. Then you just enable the alerts after saving.

Hopefully this will help you.

paupau
  • 11
  • 1
  • I tried `DisplayAlerts = WdAlertLevel.wdAlertsNone` and `wdAlertsMessageBox` (it's not a bool), but that didn't stopped the Save As dialog from opening (which I don't want to do anyway) and the COMException was still thrown. The document has never been saved locally, so the Save As dialog will always open. I havent tried your second suggestion yet of explicitly calling `.SaveAs2(...)` myself, but that really doesn't make much sense anyway. I would have to decide where to save the document for the user (temp directory?) and then the user would have to be smart enough to know where it was saved. – Joe W Oct 25 '12 at 15:22