AutoWord Sample: Demonstrates Automating Microsoft Word
The AutoWord sample includes four examples that show how to automate Microsoft Word from Visual C#. The examples demonstrate how to automate the following activities:
- Starting Word
- Creating a new document
- Opening an existing document
- Hooking events in the Word application
The sample also shows how to create an interop assembly for Microsoft Word, and shows some rudimentary techniques for automating Word.
Note One of the projects in this sample is a C++ project, so Visual C++ must also be installed.
Projects in the Sample
This sample contains five projects and produces three assemblies.
Building and Running the Sample
To create the assemblies
- Open the solution AutoWord.sln.
- Right-click the RunCreateWordAssembly project, then click Build.
- For each of the example projects, right-click the References item, then click Add Reference. Click the Browse button, select WORD.DLL from the list of .dll files, then click the Open button. Click the OK button to close the reference browser, and click the OK button for the popup window telling you that the directory WORD.DLL was found in will be added to the reference path.
- If you have installed Microsoft Word XP or Office XP, add OFFICEXP to the list of compilation constants. For each of the example projects, right-click the project root, then click Properties. Select Configuration Properties, then Build, and then add OFFICEXP to the list of Conditional Compilation Constants. Click the OK button.
Note It is not enough to define OFFICEXP in the Debug and the Release configurations. You must define OFFICEXP in each configuration.
- From the Build menu, click Build Solution.
To run the automation examples
- Right-click Example1 in Solution Explorer and select Set as Startup Project from the shortcut menu, then build and run the project.
This example shows you how to instantiate the Word object without any documents.
- Right-click Example2 in Solution Explorer and select Set as Startup Project from the shortcut menu, then build and run the project.
This example shows you how to create a new Word document and put some text into it.
- Right-click Example3 in Solution Explorer and select Set as Startup Project from the shortcut menu, then build and run the project.
This example shows you how to open an existing Word document and modify it.
- Right-click Example4 in Solution Explorer and select Set as Startup Project from the shortcut menu, then build and run the project.
This example shows you how to hook various events in Word.
Classes and Keywords
This sample demonstrates the following classes:
System.Diagnostics.Process, System.Environment, System.Reflection.Missing, System.Text.StringBuilder, System.Windows.Forms.MessageBox, System.Thread.Sleep, System.Reflection.Missing.Value, Microsoft.Win32.RegistryKey, Word.Application, Word.ApplicationEvents2_DocumentChangeEventHandler, Word.ApplicationEvents2_DocumentOpenEventHandler, Word.ApplicationEvents2_NewDocumentEventHandler, Word.ApplicationEvents2_StartupEventHandler, Word.Documents, Word._Document, Word.Range, Word.Words
This sample demonstrates the following keyword:
event