Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / C#
Article

Word document automation through .NET

Rate me:
Please Sign up or sign in to vote.
4.23/5 (22 votes)
5 Jan 20066 min read 280.4K   103   58
How to generate Word documents from a pre-formatted Word template, by populating through code.

Introduction

Recently, I was commissioned to develop an ASP.NET solution to which the client would randomly upload a Word document as a template with pre-defined 'mail merge' fields which would then be automatically populated through my code from an RDMS (in my case, MS SQL Server, but you could use just about any source of input instead) and saved elsewhere.

Background

A basic understanding of how COM works and .NET is enough for anyone to accomplish this.

Using the code

As any programmer can tell you, there are many ways of going about any task.

  • Read articles off forums and try to understand what is going on from the different forms of explanations provided.
  • Download sample code and trace it, hopefully understanding what is going on.
  • Mess around with the code yourself hoping to get lucky, and use the methods in the right order and the right way (good luck).

Well, I am hoping that you go for the former option, which would make sense, but to save you time, I will just take you step by step and explain the basics required for you to understand what is going on... you can then take it from there.

Note

Although my code samples are provided in C#, you can very easily adapt this code to any .NET language once you understand what is going on.

Assumptions

There are many things I shall have to assume for this article since they go beyond the scope of the article itself.

For the purposes of this example, I shall be assuming that you are using some flavour of the Visual Studio IDE. If you don't have one, the Express Editions of the Visual Studio 2005 are available for free off the Microsoft web site.

Getting Started

First and foremost, we must create a project to work in. What language you use is totally your choice, as is the kind of application you are going to develop. The methods used are the same, you just obviously need to map my syntax to the equivalent in your chosen language.

Now, the first thing we must do prior to developing anything that uses another technology is to 'reference' it in our project. This will allow our code to access methods in the required libraries and use them for our purposes.

I am also assuming that you already have a very basic Word document set up with a couple of Mail Merge fields already in place.

add a reference

Right click the References section in your project, select Add Reference, followed by selecting the COM tab. Scroll down the list till you find Microsoft Word x.0..., where X represents the version of Office that you have installed.

This is the COM we want to use, so just double click it and voila... you will notice that Visual Studio will automatically add the required references for you (don't you just love it?)

Time to Code

Now that we have the correct references set up, we can start coding.

The way a program references a Word document (or any Office document for that matter) is quite simple.

  1. First, we need an instance of the application in question to be running (visible or not).
  2. Then, we resume to load or create our document.
  3. We do all the editing we need and use any other feature accordingly.
  4. We save the document.

...and that's pretty much it really. So let's go over them one at a time.

Step 1 - Prepare the 'Application' and 'Document'

The first thing we want to do is add a 'using' clause to our code, this will make references to the required methods far easier since we won't have to stay qualifying them each time.

C#
//
// First add a direct reference to the Word libraries,
// this will make referencing them far easier within our code
//
using System;
using Microsoft.Office.Interop.Word;

The next thing we want to do is create a Word 'Application' referencing object and a 'Document' object which we shall be using to allow us to interface with the Word application itself, giving instructions as to what should be done to the document.

So, here we go...

C#
//
// Declare your class, and then declare your objects

public class MyClass
{
    private ApplicationClass myWordApp = new ApplicationClass();  // our application
    private Document         myWordDoc = new Document();          // our document

Now, all we need to do is use these objects to do our biddings.

Step 2 - Load our Document Template

Now, we just need to tell Word from where to load our template...

C#
//
// method which replaces our fields with whatever text
//


private void ParseWord()
{
   object nothing     = System.Reflection.Missing.Value; // our 'void' value
   object filename    = "C:\MyWordTemplate.dot"; // our word template
   object destination = "C:\MyNewDocument.doc";  // our target filename
   object notTrue     = false;  // our boolean false

   
   string myText      = "Hello World!!";
   // the sample text we want as replacement

The above declarations are the basic objects we shall be using as parameters to send to our Word application each time we invoke any method. Since we are speaking COM, we are therefore also speaking 'object'.

Also, note the declaration of 'nothing'. We use this for parameters that we are not interested in addressing. So rather than trying to figure out the correct parameters, we need only concentrate on the ones that really affect us.

Next, we want to:

  • tell Word not to show itself,
  • tell Word to load the template,
  • see how many fields we have in this document.

So, here goes...

C#
//
// now we want to load the template and check how many fields there are to replace
//

myWordApp.Visible = false;  // tell word not to show itself

myWordDoc = myWordApp.Documents.Add( // load the template into a document workspace
                      ref filename,  // and reference it through our myWordDoc
                      ref missing,
                      ref missing,
                      ref missing);

int fields = myWordDoc.Count;
// count how many fields we have to update

Note, we first told Word not to show itself prior to doing anything, you want this if you plan on creating documents, otherwise your workstation might start acting a little bit weird. You can always set this to true should you want to see what is going on, but I generally see it as far more professional if the end user doesn't see the application itself (after all, that is the beauty of automation).

If you delve further into the object, you will find multiple (very intuitive) parameters which you can add, such as read-only, passwords etc... you can set these using the same style above, ex. object myTrue = true for a boolean false.

Step 3 - Let's do our editing

Next, we are counting how many fields we have that can be updated, this can be done through the myWordApp object, but it is far easier to access it directly through the myWordDoc (which is why we created it).

Next, we want to change all the fields to our text (for the purposes of this example).

C#
//
// now we can simply iterate through the fields collection and update
//

foreach (Field myField in myWordDoc.Fields) 
{ 
  myField.Select(); 
  myWordApp.Selection.TypeText( myText );
  // and voila... we changed the text!!
}

And that is it really, we have replaced all the fields with our default text. Of course, you will hopefully want to make some more complex updates rather than just pasting a string in, but you should now have a rather good understanding of how all this works.

Also, note the use of the 'Field' data type, this is also part of the COM object and denotes a field. There is a property with this type called 'Result' which stores the current default text in the field. Rather than using the 'Select()' option, we could have just written myField.Result = myText, yet this would still leave it as a field in your new document, which is why I opt for the 'Select()' method instead (which basically is like highlighting the text in Word and typing over it).

Step 4 - Save...and quit

Last but not least, we now want to save our new document and close Word.

It is important to remember to close Word, since it is actually open in the background, just not visible.

C#
//
// now the last touch.. save and close
//
myWordDoc.SaveAs( 
         ref destination, 
         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); 
      
// quit word
myWordApp.Application.Quit(
         ref notTrue, 
         ref missing, 
         ref missing);

That is all, we now have a new document with updated fields. Neat huh?

Conclusion

As outlined earlier, the purpose of this article is more of an explanation of how to interface with Word rather than a real life example. I seriously doubt you would want to just replace the same string in every field of a document. Yet hopefully, this simple example has given you enough understanding of how things work.

History

  • v1.0 - Saturday 12th November 2005.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Malta Malta
I have been fiddling in software since when I was around 10 years old... ...and I still am Blush | :O )

Comments and Discussions

 
QuestionmyWordApp.Selection.TypeText Pin
Jaineet Malhi6-Nov-14 20:16
Jaineet Malhi6-Nov-14 20:16 
GeneralMy vote of 3 Pin
Menon Santosh13-Feb-13 0:38
professionalMenon Santosh13-Feb-13 0:38 
QuestionDisposing objects Pin
nicolas200819-Nov-12 23:05
nicolas200819-Nov-12 23:05 
Generalthere are unnecessary parts in code [modified] Pin
Halil Güneş16-Oct-08 21:56
Halil Güneş16-Oct-08 21:56 
GeneralError: Word was unable to read this document Pin
Tselvi25-Mar-08 21:12
Tselvi25-Mar-08 21:12 
GeneralRe: Error: Word was unable to read this document Pin
dahalbibek18-Apr-08 0:20
dahalbibek18-Apr-08 0:20 
GeneralRe: Error: Word was unable to read this document Pin
Jakes_GG25-Nov-08 0:52
Jakes_GG25-Nov-08 0:52 
GeneralError: The name 'missing' does not exist in the current context Pin
Tselvi25-Mar-08 20:42
Tselvi25-Mar-08 20:42 
GeneralRe: Error: The name 'missing' does not exist in the current context Pin
Tselvi25-Mar-08 21:10
Tselvi25-Mar-08 21:10 
QuestionCan we import sql tables to MS word ?? Pin
niting8530-Jan-08 2:57
niting8530-Jan-08 2:57 
AnswerRe: Can we import sql tables to MS word ?? Pin
Johnny Glenn15-Mar-12 23:35
Johnny Glenn15-Mar-12 23:35 
Generalword file prepartion from c# web application Pin
ram on c#9-Nov-07 18:23
ram on c#9-Nov-07 18:23 
Generalword automation on windows 2003 server Pin
wildox14-Jun-07 0:44
wildox14-Jun-07 0:44 
QuestionMissing library Pin
Marco19755-Jun-07 23:41
Marco19755-Jun-07 23:41 
AnswerRe: Missing library Pin
Marco19755-Jun-07 23:56
Marco19755-Jun-07 23:56 
GeneralRe: Missing library [modified] Pin
mishas15-Aug-07 21:29
mishas15-Aug-07 21:29 
GeneralRe: Missing library Pin
malli1225200216-Sep-09 2:02
malli1225200216-Sep-09 2:02 
Generalmay be i am missing the library Pin
Mohammad Al Hoss23-May-07 5:02
Mohammad Al Hoss23-May-07 5:02 
GeneralRe: may be i am missing the library Pin
Stefan Z Camilleri23-May-07 8:10
Stefan Z Camilleri23-May-07 8:10 
QuestionHow to retrieve Word/Exell documents and save into database Pin
npkinh17-May-07 22:40
npkinh17-May-07 22:40 
AnswerRe: How to retrieve Word/Exell documents and save into database Pin
Stefan Z Camilleri17-May-07 22:51
Stefan Z Camilleri17-May-07 22:51 
Generalinsufficient memory Pin
satejprabhu16-Mar-07 23:28
satejprabhu16-Mar-07 23:28 
QuestionRe: insufficient memory Pin
jvanbuye3320-Nov-08 9:31
jvanbuye3320-Nov-08 9:31 
Has there been a resolution found for this error ??
QuestionPopulate merge fields with database values ? Pin
Member 265207822-Feb-07 14:22
Member 265207822-Feb-07 14:22 
Generalproblem with sending a table to word (office 07) [modified] Pin
giddy_guitarist18-Dec-06 4:31
giddy_guitarist18-Dec-06 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.