Click here to Skip to main content
15,886,258 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 281K   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

 
QuestionTransfering this code to Windows Server 2003 Pin
northturton6-Sep-06 10:21
northturton6-Sep-06 10:21 
AnswerRe: Transfering this code to Windows Server 2003 Pin
Stefan Z Camilleri6-Sep-06 10:33
Stefan Z Camilleri6-Sep-06 10:33 
GeneralActionsPane Pin
dead smed20-Jul-06 16:08
dead smed20-Jul-06 16:08 
GeneralError in VS 2005 Pin
filimon27-Feb-06 4:19
filimon27-Feb-06 4:19 
Generalfile in memory Pin
Jan Kučera31-Jan-06 8:48
Jan Kučera31-Jan-06 8:48 
GeneralRe: file in memory Pin
Robert Hutch22-Feb-12 23:38
Robert Hutch22-Feb-12 23:38 
QuestionMultiple pages Pin
peterb7413-Jan-06 3:39
peterb7413-Jan-06 3:39 
JokeAlternatives Pin
alisoylu11-Jan-06 9:09
alisoylu11-Jan-06 9:09 
Quote: "Microsoft strongly recommends that developers find alternatives to Automation of Office if they need to develop server-side solutions. Because of the limitations to Office's design, changes to Office configuration are not enough to resolve all issues. Microsoft recommends a number of alternatives that do not require Office to be installed server-side, and that can perform most common tasks more efficiently and quickly than Automation. Before involving Office as a server-side component in your project, consider alternatives. "
Source: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757

I've used the method described in http://support.microsoft.com/kb/270906/ and have found it to be faster and more reliable. (I had too many troubles running word on the server side - processes not terminating, word hanging asking the user a question...) However I do not manually write the rtf from code, instead I extract my data as XML from the databse, and use an XSL to transform it into a RTF. (just like a mail merge) Explained here: http://support.microsoft.com/default.aspx?scid=kb;en-us;311461
GeneralRe: Alternatives Pin
Stefan Z Camilleri11-Jan-06 9:17
Stefan Z Camilleri11-Jan-06 9:17 
GeneralRe: Alternatives Pin
alisoylu11-Jan-06 9:19
alisoylu11-Jan-06 9:19 
GeneralRe: Alternatives Pin
kennster12-Jan-06 4:15
kennster12-Jan-06 4:15 
GeneralRe: Alternatives Pin
alisoylu12-Jan-06 4:21
alisoylu12-Jan-06 4:21 
GeneralRe: Alternatives Pin
TiagoMartins25-May-07 5:29
TiagoMartins25-May-07 5:29 
GeneralRe: Alternatives Pin
Novakovi3-Nov-14 22:12
Novakovi3-Nov-14 22:12 
GeneralWord Safe Mode Pin
stormcroww11-Jan-06 8:10
stormcroww11-Jan-06 8:10 
GeneralMultiple version of Word Pin
RMBennett7210-Jan-06 11:50
RMBennett7210-Jan-06 11:50 
AnswerRe: Multiple version of Word Pin
Stefan Z Camilleri10-Jan-06 12:03
Stefan Z Camilleri10-Jan-06 12:03 
GeneralRe: Multiple version of Word Pin
GaryWoodfine 10-Nov-06 1:38
professionalGaryWoodfine 10-Nov-06 1:38 
GeneralInsufficient Pin
chaldon9-Jan-06 22:24
chaldon9-Jan-06 22:24 
QuestionWell Done Pin
JohnDeHope35-Jan-06 16:19
JohnDeHope35-Jan-06 16:19 
AnswerRe: Well Done Pin
Stefan Z Camilleri6-Jan-06 14:34
Stefan Z Camilleri6-Jan-06 14:34 
GeneralMinor fix... Pin
Paul Conrad5-Jan-06 13:37
professionalPaul Conrad5-Jan-06 13:37 
AnswerRe: Minor fix... Pin
Stefan Z Camilleri6-Jan-06 14:36
Stefan Z Camilleri6-Jan-06 14:36 
GeneralRe: Minor fix... Pin
Paul Conrad6-Jan-06 15:24
professionalPaul Conrad6-Jan-06 15:24 
Questionwhat when i use word 2000 or 2002 Pin
harryOld++5-Jan-06 12:48
harryOld++5-Jan-06 12:48 

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.