Managed Extensions: Using the Microsoft Word Spell Checker via Automation


How to Help Your Business Become an AI Early Adopter

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.

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A CRM solution holds a wealth of information and document generation tools allow users to take that information and create documents with both visual appeal and function. Document generation is the process of automatically producing a file and document generation applications save companies time, mistakes, and money. You bought Salesforce to be more efficient — why are you still manually creating proposals, contracts, invoices, and account plans? Read this eBook to learn how you can automate virtually …

  • In order for IT service providers to succeed, it's paramount that they find a competitive advantage and continually develop new ways to find additional revenue streams. IT service providers need to be able to do it all for their clients – from managing entire technology infrastructures to responding quickly to a multitude of end-user needs. With a growing number of issues to resolve and limited technicians at hand, how can IT service providers operate efficiently while providing top-notch service …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date