Microsoft & .NET Visual C# Managed Extensions: Using the Microsoft Word Spell Checker via Automation

Managed Extensions: Using the Microsoft Word Spell Checker via Automation

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

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

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

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

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

  9. 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).

  10.  

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

  13. Add a button to perform the spell check.
  14.  

  15. 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.
  16.  

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

    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.

Latest Posts

Related Stories