Automating Microsoft Word 2003

Automating Microsoft Word 2003

You're now going to build another simple application that automates Microsoft Word 2003. Begin by creating a new project titled Automate Word. Right-click Form1.cs in the Solution Explorer, choose Rename, and then change the default form's name to frmMain.cs. Next, change the form's Text property to Automate Word.

Creating a Reference to an Automation Library

To automate Microsoft Word, you have to reference Word's object libraryjust like you did for Excel. Follow these steps to reference the library:

Display the Add Reference dialog box by choosing Project, Add Reference.

Click the COM tab to display the available COM components (programs that have a type library) on your computer.

Scroll the list and locate the Microsoft Word 11 Object Library (see Figure). Double-click the Excel item to create a reference to it and close the dialog box.

3. You have to reference Word's Automation Library to use it in code.

By the Way

If you don't see Microsoft Word 11.0 Object Library in your list of available COM references, you probably don't have Word 2003 installed, and this example will not work.

Creating an Instance of an Automation Server

As with the previous example, all the code for automating Word is placed in the Click event of a button, but first you have to add a using statement. Follow these steps to create the button and associated code:

Add a button to the form now by double-clicking the Button item in the tool-box and set the button's properties as follows:










Automate Word

Double-click the button to access its Click event.

Scroll to the top of the class and add this statement right below the existing using statements:

using Word;

To work with Word's object model, you have to have an instance of Word's Application object. Put your cursor back in the btnAutomateWord_Click event and enter the following statement to create a variable that contains an instance of Word's Application object:

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

As with Excel, Word starts up hidden, so the user doesn't know it's running. Because you're going to want to see the fruits of your labor, add this statement to force Word to show itself:

objWord.Visible = true;

Next, you need to have Word create a new document. This is accomplished using the Add() method of the Documents collection. This method expects four optional parameters. Since they're optional, you're not going to pass them any data. However, you still have to include the arguments in your call to the method. Rather than use System.Reflection.Missing.Value for each optional parameter, which is essentially like leaving out an argument like you did in the Excel example, you'll create a variable to hold this value and use the variable in the call to Add(). Enter these statements to declare the variables and create the new document:

Word.Document objDoc;
object objMissing = System.Reflection.Missing.Value;

objDoc = objWord.Documents.Add(ref objMissing, ref objMissing,
   ref objMissing, ref objMissing);

There are many ways to send text to Word. Perhaps the easiest is the TypeText() method of the Selection object. The Selection object refers to currently selected text in the Word document. When a new document is created, there is no text, and the selection object simply refers to the edit cursor at the start of the document. Sending text to Word using Select.TypeText() inserts the text at the top of the document. Enter this statement to send text to Word:

objWord.Selection.TypeText("This is text from a C# 2005 application.");

The last statement you need to enter sets the Word object to null to release the reference to it:

objWord = null;

Now, press F5 to run the program. You should see Word start, and then a new document is created using the text you specified with TypeText() (see Figure).

4. A simple but effective demonstration of automating Word.

By the Way

Automating applications, particularly Office products such as Excel and Word, requires a lot of system resources. If you intend to perform a lot of automation, you should use the fastest machine with the most memory that you can afford. Also, be aware that for automation to work, the server application (Excel or Word in this case) has to be installed on the user's computer in addition to your application.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows