How to Create a Microsoft Word Document with C#

Creating, Updating and Saving a Word File by Using C# Code

How to Create a Microsoft Word Document with C# - Mark Alexander Bain
How to Create a Microsoft Word Document with C# - Mark Alexander Bain
A C# programmer can quickly automate the creation of a Microsoft Word document. With just a few lines of code they can add paragraphs as save the file to the computer.

C# is an excellent language for automating processes. One reason for that is the ease with which programmers can control the running of other applications such as Microsoft Word. With just a few lines of code the programmer is able to:

  • run Microsoft Word
  • create a new Microsoft Word document
  • write to the new document
  • save the document to the user’s computer
  • close Microsoft word

And this can all be done either in full view of the user (so that they can see what is going on) or it can be done completely invisibly.

Adding Microsoft Word as a C# Reference

There is, of course, an initial assumption that Microsoft Word is actually installed on the programmer’s computer. If it is then they will need to add it as a reference (as shown in figure 1 at the bottom of this article). Once that’s been done then the programmer can start writing the code that will control Word.

Running Microsoft Word with C#

The C# application developer starts Microsoft Word by creating a new Word application object:

Word.Application oWord = new Word.Application();

By default Microsoft Word will be run invisibly in the background. However, it can be useful to see the document during the development phase of the project:

oWord.Visible = true;

This code will make Word visible. It’s then just a matter of removing (or commenting out) this line once the application is ready for its users.

Creating a New Microsoft Word Document

Once Microsoft Word has been opened then a new document can be added to it. However, the method to do that requires a number of values, many of which are known or needed by the programmer. However, the programmer just needs to create an object that will handle these missing values:

object oMissing = System.Reflection.Missing.Value;

And then this object can be used when the new document is created:

Word.Document oDoc = oWord.Documents.Add (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

If the application is run at this point then a new, blank document will be created. The next step is to populate the document with useful information.

Adding Paragraphs to a Microsoft Word Document with C#

The C# Programmer adds content to a Microsoft Word document by adding paragraphs:

Word.Paragraph oPara1;
oPara1 = oDoc.Paragraphs.Add(ref oMissing);

Any formatting of the paragraph can take place at this point. For example the paragraph can be turned into a heading:

object styleHeading1 = "Heading 1";
oPara1.Range.set_Style(ref styleHeading1);

And, of course, the programmer can add text to the paragraph:

oPara1.Range.Text = "Create a New Report";

If further paragraphs are to be added then one final statement is required:

oPara1.Range.InsertParagraphAfter();

This statement ensures that adding further paragraphs will not overwrite the existing one. Any new paragraphs are added in exactly the same way:

Word.Paragraph oPara2;
oPara2 = oDoc.Paragraphs.Add(ref oMissing);
oPara2.Range.Text = "This is my first paragraph.";
oPara2.Range.InsertParagraphAfter();
Word.Paragraph oPara3;
oPara3 = oDoc.Paragraphs.Add(ref oMissing);
oPara3.Range.Text = "This is my second paragraph.";
oPara2.Range.InsertParagraphAfter();

In the example above the new paragraphs will just have the default formatting (as can be see in figure 2).

Saving a Microsoft Word Document with C#

Once the document contains all of the necessary information then it can be saved:

object fileName = "C:\\Reports\\daily_report.doc";
oDoc.SaveAs (ref fileName,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

However, it should be noted that this overwrites an existing file of the same name.

Closing a Microsoft Word Document

Finally the document can be closed:

oDoc.Close (ref oMissing, ref oMissing, ref oMissing);

As can the Word application:

oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
oWord = null; // Free up any memory used

At this point the compiler may give an ambiguity warning. This is because there are both close and quit methods and events. However, the compiler will resolve the ambiguity itself and the application will work as expected.

And, at the end of the process the application will have produced a new Microsoft Word document that is fully formatted and contains the information specified by the programmer.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement

Comments

Jan 7, 2010 3:28 AM
Guest :
Very Nice Article..
Jun 29, 2010 1:18 AM
Guest :
good article
2 Comments
Advertisement
Advertisement