October 21, 2018
Hot Topics:

Create and Edit Office Documents with .NET

  • May 4, 2005
  • By Dino Esposito
  • Send Email »
  • More Articles »

The following code saves a Word document:

doc.SaveAs(ref fileToSave,
      ref Missing, ref Missing, ref Missing, ref Missing,
      ref Missing, ref Missing, ref Missing, ref Missing,
      ref Missing, ref Missing, ref Missing, ref Missing,
      ref Missing, ref Missing, ref Missing);

Be sure to close both the document and the Word application before you quit your application. You can do that as follows:

doc.Close(ref Missing, ref Missing, ref Missing);
app.Quit(ref Missing, ref Missing, ref Missing);

Lastly, you need to know how to create a new Word document. After getting a new application instance, enter the following:

Word.Document doc = app.Documents.Add(
ref objMissing, ref objMissing,
ref objMissing, ref objMissing);

You first add a new document to the Documents collection of the application and activate it. Next, you start typing text using the methods of the Selection object:

app.Selection.TypeText("This is a sample Word document");

No Easy Task

Creating and editing Office documents from within .NET applications is not as easy as it should be. The main obstacle remains the different programming models—COM versus .NET—that wrapper tools can't always hide. Add to this the inherent complexity of the Word (or Excel) object models and you get a clear picture of why programming Office applications is complex and boring.

Whichever approach you choose, you can't perform any step without a good understanding of the Office applications object models. In addition, if you use VSTO, strong knowledge of .NET security and deployment issues is required.


About the Author

Dino Esposito is Wintellect's ADO.NET expert and a trainer and consultant based in Rome, Italy. He runs the Cutting Edge column for MSDN Magazine and is a regular contributor to MSDN News, Visual C++ Developers Journal, and SQL Server Magazine.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date