Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions.

Many times in our programming lives we find that some features from another application would be extremely beneficial within our own software. One great example of that is the Microsoft Word spell checker. While many C# and VB.NET examples illustrate Automation from a .NET application, I couldn't find one that showed how to automate Word from Managed C++. In addition, since I ran into several "gotchas", I thought that this task would make a nice addition to the .NET Tips & Techniques series.

The following is a step-by-step demo of how to use Automation to access the Microsoft Word spell checker from a Managed C++ application. (The accompanying demo application allows you to quickly test these steps.)

Create a new C++ Windows Forms application. I named mine OfficeWord.

From the Solution Explorer, right-click References and then click Add References.

When the Add References dialog box appears, click the COM tab.

Locate the entry for "Microsoft Word 11.0 Object Library" and click the Select button, followed by the OK button. This will add the necessary references to your .NET project.

Add the following using statement to the top of your form code:

using namespace Microsoft::Office::Interop;

Note: The class that you use to automate Microsoft Word is actually in the Microsoft::Office::Interop::Word namespace. However, that namespace includes an interface called System that will conflict with the .NET System namespace. As a result, I include the Microsoft::Office::Interop namespace and qualify the objects within that namespace with the Word namespace name (e.g., Word::ApplicationClass).

Add a text box (edit control) to the form that will contain the value to be spell checked.

Add a button to perform the spell check.

Now you need to figure out how to use the Word objects. The easiest way to determine which classes, method, and properties are available from a reference is to use the Visual Studio ObjectBrowser. From the Visual Studio View menu, select Object Browser.

Expand the Microsoft.Office.Interop.Word entry as shown in the following figure:

You'll notice that the Application class (selected) has a COM class name of ApplicationClass. Knowing this will come in handy shortly. Also note the members of the Application class in the right pane, including the CheckSpelling method. As you can see, the Object Browser is a great way to spelunk through COM objects to determine their functionality. This is especially useful in situations where the objects are not documented very well.

Now that you know the name of the COM object and the method you want to call, add a handler for the spell check button and code it as follows (You must use the COM class name of ApplicationClass in order to avoid compiler errors.):

Note: The Word object's CheckSpelling method takes a large number of arguments. In order to indicate that you don't wish to pass values for those arguments, you can simply use the System::Reflection::Missing object.

Build and run your application to see results similar to those shown in the following
figure:

Final Notes

The focus of this article was to illustrate how to use Automation from Managed C++. In that vain, I used one parameter of one method of the incredibly rich and robust Word object. To read more about the different parameters you can use with the CheckSpelling method (such as specifying a custom dictionary, ignoring case, etc.) and to view what else you can automate from the various Office products, refer to the MSDN Web site.

Download the Code

To download the accompanying source code for this article, click here.

Related Articles

About the Author

The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.