6

Just wondering if anyone knows how to not save a document after creation when using Microsoft.Office.Interop.Word.

Basically i am using mail merge to print off a report from c#, the document get made, values are passed to it no problem and i can then print it without any issues, however, I only need to print the document, I do not need to save a copy on the computer.

Once i have passed all the values to the document, i use the following code to print and close the document:

wordDoc.PrintOut();
wordDoc.Close();
wordApp.Application.Quit();

However, this prints the document and then the 'Save' dialog pops up, asking where to save the document. I dont want this as clicking cancel produces an error (and requires another unwanted interaction from the user), and i dont need to save the file.

Any ideas?

Using Visual Studio 2012, .NET 4.0, and i have tried using the following files types for the office document: .doc, .dot, .docx, .dotx.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shaun
  • 175
  • 1
  • 2
  • 8

4 Answers4

9

You can pass SaveChanges parameter as wdDoNotSaveChanges to the Close method to skip the saving part.

Have a look at:

how to close a running instance of Word document? (C#)

MSDN Reference.

Community
  • 1
  • 1
publicgk
  • 3,170
  • 1
  • 26
  • 45
6

I know this is a very late response to Johns question, but I also needed to close an edited document without having that save dialog displayed.

I found this method worked perfectly for me:

// Tells the application not to save changes    
((_Application)wordDoc.Application).Quit(false);
Overdrive77
  • 115
  • 2
  • 9
1

How about suppressing alerts?

Word.Application word = new Word.Application();
word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
toby
  • 885
  • 3
  • 10
  • 21
0

I know this is very late but you can try this one.

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: true, Visible: true);
Microsoft.Office.Interop.Word.WdSaveOptions dontSave = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 
aDoc.Close(dontSave, null, null);
Orace
  • 7,822
  • 30
  • 45
James Dean
  • 13
  • 4