Managed Extensions: Using the Microsoft Word Spell Checker via Automation

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • Read the ebook Hybrid Cloud & Data Fabric for Dummies to learn how a Data Fabric can ease data management complexity within a hybrid cloud and help you maximize the control of your data — from anywhere at any time. Find out how to: Seamlessly and dynamically move data across private and public cloud resources Keep data secure and minimize business disruptions …

  • MongoDB has the flexibility, adaptability and extensibility to embrace widely varying data types and rapid design/deployment cycles. Combining MongoDB with our 5100 ECO Enterprise SSD brings amazing results in smaller, simpler deployments compared to legacy storage. In this technical brief, we compare two MongoDB test clusters, each using the Linux Logical Volume Manager (LVM) for RAID configuration: 5100 ECO 3-node cluster: Two Micron 5100 ECO (1.92TB) per node configured as a software RAID 0 (LVM) Legacy …

Most Popular Programming Stories

More for Developers

RSS Feeds

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