March 10, 2011, 11:35 p.m.
posted by seele
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:
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:
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.