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.

Thanks
Shaun

Recommended Answers

All 6 Replies

Did you try Dispose?

No dispose method unfortunatley.

Word is probably using it's default behaviour, when you try to close a document without saving it.

Try this, wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);.

Pass "false" in the Close method;

wordDoc.Close(false);

Sorry forgot C#,

object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
object missing =  System.Reflection.Missing.Value;
doc.Close(ref doNotSaveChanges, ref missing, ref missing);
commented: top +14

Brilliant, worked great, thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.