Export (0) Print
Expand All
5 out of 12 rated this helpful - Rate this topic

AutoWord Sample: Demonstrates Automating Microsoft Word

Visual Studio .NET 2003

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

  1. Open the solution AutoWord.sln.
  2. Right-click the RunCreateWordAssembly project, then click Build.
  3. 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.
  4. 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.
  5. From the Build menu, click Build Solution.

To run the automation examples

  1. 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.

  2. 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.

  3. 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.

  4. 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

See Also

Visual C# Samples | General Samples

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.