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. 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)
    {
      try
      {
        Word::ApplicationClass* winword = new
                                Word::ApplicationClass();
        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
                                     System::Text::StringBuilder();
        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)
      {
        MessageBox::Show(e->Message);
      }
    }
    
    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.

Downloads

Comments

  • 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

  • Learn about the state of the nation of Dell EMC Data Protection. What are the current Data Protection challenges our customers are facing? What are the unique benefits of the Dell EMC Data Protection solution? How do we assist our customers and partners with their effort to Modernize, Automate, and Transform their IT environments through efficient and innovative solutions? The discussion will cover protecting virtual environments, cloud based applications, and how to gain the efficiencies of cloud storage for …

  • You know that visibility is critical to security. You simply can't secure what you can't see. Read this Gartner research note to learn why visibility through a continuous adaptive risk and trust assessment (CARTA) approach is key to securing digital business. Download now to learn eight ways this approach can be deployed to optimize the visibility into your organization's cloud services, mobile endpoints, and digital ecosystems.

Most Popular Programming Stories

More for Developers

RSS Feeds

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