Windows Forms: Automate Office

This demonstration shows you how to automate Office from .NET.

Featured Highlights

This application has three demonstrations involving the following Office components. When referencing the various COM Office assemblies, Visual Studio .NET automatically creates COM runtime callable wrappers (RCW) so you can program against them from within .NET. This application shows you how to work with:

1. Microsoft Agent: Learn how to make Merlin appear in your applications and do things like speak, move, and show off his various animations. Merlin and the other Office Agent characters are an excellent way to provide Help and other assistance to users of your application.

2. Microsoft Word: Learn how to create a Word application and call the spell checker to check a document loaded into a RichTextBox control. 3. Microsoft Excel: Learn how to create and fill a DataSet from an XML document and then databind it to a DataGrid. Then export the contents of the DataSet to an Excel spreadsheet and run an Excel function that calculates the average of values in a column.Microsoft Agent: Learn how to make Merlin appear in your applications and do things like speak, move, and show off his various animations. Merlin and the other Office Agent characters are an excellent way to provide Help and other assistance to users of your application. For more on Microsoft Agent see the articles and SDK documentation at http://msdn.microsoft.com. 2. Microsoft Word: Learn how to create a Word application and call the spell checker to check a document loaded into a RichTextBox control.

3. Microsoft Excel: Learn how to create and fill a DataSet from an XML document and then databind it to a DataGrid. Then export the contents of the DataSet to an Excel spreadsheet and run an Excel function that calculates the average of values in a column.

Requirements

Microsoft Visual Studio.NET Professional or greater.
Windows 2000 or Windows XP.

If you want to hear Merlin speak, you may need to install the speech recognition pack, which is included with Microsoft Office XP but not installed by default. For instructions on how to do this see:
http://www.microsoft.com/technet/treeview/default.asp?url=/TechNet/prodtechnol/winxppro/proddocs/input_speech_install.asp

Running the Sample

Depending on your Office installation configuration, the Office references may not be valid in the project. To update the references:

  1. Open the References folder in Solution Explorer.
  2. Note the name of unresolved reference. Unresolved references are indicated by a warning icon.
  3. Right-click the reference and select Remove.
  4. Right-click the References node and select Add Reference. The Add Reference dialog box appears.
  5. Select the COM tab. The Office libraries used in this project are COM libraries.
  6. Find and select the library.
  7. Click OK to add the reference to the project.

Press F5 to run the sample.

See also

For more on Microsoft Agent see the articles and SDK documentation at http://msdn.microsoft.com. A good place to start is:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_msagent-i.asp.