Managed Extensions: Using the Microsoft Word Spell Checker via Automation

Friday Nov 12th 2004 by Tom Archer - MSFT

Learn how to access the Microsoft Word spell checker using Automation from your own Managed C++ Windows Forms applications.

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. Whereas 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, because 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. From the Solution Explorer, right-click References and then click Add References.

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

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

  5. 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 (for example, Word::ApplicationClass).

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

  7. Add a button to perform the spell check.

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

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

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

  11. 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.):
    private: System::Void button1_Click(System::Object *  sender,
                                        System::EventArgs *  e)
        Word::ApplicationClass* winword = new
        Object* o = System::Reflection::Missing::Value;
        bool success = winword->CheckSpelling(textBox1->Text, &o,
                                              &o, &o, &o, &o,&o, &o,
                                              &o, &o, &o, &o, &o);
        System::Text::StringBuilder* str = new
        str->AppendFormat(S"Your text has {0} errors",
                          success == true ? S"NO" : S"spelling");
        MessageBox::Show(str->ToString(), S"Spell Check Results");
      catch(Exception* e)
    Note: The Word object's CheckSpelling method takes a large number of arguments. To indicate that you don't want to pass values for those arguments, you simply can use the System::Reflection::Missing object.

  12. 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 vein, 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, and so on) and to view what else you can automate from the various Office products, refer to the MSDN Web site.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved