Walkthrough: Accessing the Spelling Checker in Word
Mary Chipman
MCW Technologies, LLC
September 2003
Applies to:
Microsoft® Visual Studio® Tools for the Microsoft Office System
Microsoft Office Word 2003
Microsoft Visual Studio .NET 2003
Summary: Demonstrates how to access the Word spelling checker by invoking the built-in collection of Dialog objects in Word. (8 printed pages)
Contents
Introduction
Prerequisites
Getting Started
Creating the Spelling Checker Procedure
Creating a CommandBar Menu Item
Conclusion
Introduction
In this walkthrough, you first create a procedure to invoke the Microsoft® Office Word spelling checker dialog box. Then, you create a CommandBar menu item and a CommandBarButton to invoke the procedure you have written that launches the spelling checker.
Prerequisites
To complete this walkthrough, the following software and components must be installed on the development computer:
- Microsoft Visual Studio® .NET 2003 or Microsoft Visual Basic® .NET Standard 2003
- Microsoft Visual Studio Tools for the Microsoft Office System
- Microsoft Office Professional Edition 2003
**Tip **This demonstration assumes that if you're a Visual Basic .NET programmer, you've set the Option Strict setting in your project to On (or have added the Option Strict statement to each module in your project), although it is not required. Setting the Option Strict setting to On requires a bit more code, as you see, but it also ensures that you do not perform any unsafe type conversions. You can get by without it, but in the long run, the discipline required by taking advantage of this option far outweighs the difficulties it adds as you write code.
Getting Started
First you need to create a Word Document project using Visual Studio Tools for the Microsoft Office System.
To create a Word Document project
Start Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the Project Types pane, expand Microsoft Office System Projects, and then select Visual Basic Projects or Visual C# Projects.
In the Templates pane, select Word Document.
Name the project WordSpellChecker, and store it in a convenient local path.
Accept the defaults in the Microsoft Office Project Wizard, and click Finish to create the project.
Visual Studio .NET opens the ThisDocument.vb or ThisDocument.cs file in the Code Editor for you.
Creating the Spelling Checker Procedure
The procedure to invoke the Word spelling checker uses the wdDialogToolsSpellingAndGrammarWdWordDialog enumeration to display the spelling checker dialog box.
To create the spelling checker procedure
Write code to create a Dialog object variable using the wdDialogToolsSpellingAndGrammar enumeration. Use the Show method to display the dialog box to the user:
' Visual Basic Private Sub CheckSpelling() Dim dlg As Word.Dialog dlg = ThisApplication.Dialogs.Item( _ Word.WdWordDialog.wdDialogToolsSpellingAndGrammar) dlg.Show() End Sub // C# private void CheckSpelling() { Word.Dialog dlg; Object timeOut = Type.Missing; dlg = ThisApplication.Dialogs[ Word.WdWordDialog.wdDialogToolsSpellingAndGrammar]; dlg.Show(ref timeOut); }
Create the Click event handler for the Spell Check menu item you'll create in the next section, so that it calls the CheckSpelling procedure:
' Visual Basic Private Sub MenuItem_Click( _ ByVal Ctrl As Office.CommandBarButton, _ ByRef CancelDefault As Boolean) CheckSpelling() End Sub //C# private void MenuItem_Click( Office.CommandBarButton Ctrl, ref bool CancelDefault) { CheckSpelling(); }
Creating a CommandBar Menu Item
At this point you have the code to invoke the spelling checker, but have no way for the user to call your code. You need to create a menu item to call your code.
To create a menu item
Add the following declarations to the OfficeCodeBehind class, immediately following the existing declarations for the ThisDocument and ThisApplication variables:
' Visual Basic Private Const MENU_TAG As String = "DEMO_CODE" // C# private const String MENU_TAG = "DEMO_CODE";
Create the procedure to create the Demo Code menu item, and its Spell Check child:
' Visual Basic Private Sub CreateMenus() ' Set up Command Bars. Dim MainMenuBar As Office.CommandBar Dim MenuBarItem As Office.CommandBarPopup Try MainMenuBar = ThisApplication.CommandBars( _ "Menu Bar") ' Attempt to find the existing menu bar item. MenuBarItem = DirectCast(MainMenuBar.FindControl( _ Office.MsoControlType.msoControlPopup, Tag:=MENU_TAG), _ Office.CommandBarPopup) If MenuBarItem Is Nothing Then MenuBarItem = DirectCast(MainMenuBar.Controls.Add( _ Office.MsoControlType.msoControlPopup), _ Office.CommandBarPopup) MenuBarItem.Caption = "&Demo Code" MenuBarItem.Visible = True MenuBarItem.Tag = MENU_TAG Dim cbb As Office.CommandBarButton cbb = DirectCast(MenuBarItem.Controls.Add( _ Office.MsoControlType.msoControlButton, _ Temporary:=True),Office.CommandBarButton) cbb.Caption = "Spell Check" cbb.Visible = True AddHandler cbb.Click, AddressOf MenuItem_Click End If Catch ex As Exception MessageBox.Show(ex.Message, _ ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub // C# private void CreateMenus() { // Set up Command Bars. Office.CommandBar MainMenuBar; Office.CommandBarPopup MenuBarItem; try { MainMenuBar = ThisApplication.CommandBars["Menu Bar"]; // Attempt to find the existing menu bar item. MenuBarItem = (Office.CommandBarPopup) MainMenuBar.FindControl( Office.MsoControlType.msoControlPopup, Type.Missing, MENU_TAG, Type.Missing, Type.Missing); if ( MenuBarItem == null ) { MenuBarItem = (Office.CommandBarPopup) MainMenuBar.Controls.Add( Office.MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, Type.Missing); MenuBarItem.Caption = "&Demo Code"; MenuBarItem.Visible = true; MenuBarItem.Tag = MENU_TAG; Office.CommandBarButton cbb; cbb = (Office.CommandBarButton)MenuBarItem.Controls.Add( Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true); cbb.Caption = "Spell Check"; cbb.Visible = true; cbb.Click += new Microsoft.Office.Core. _CommandBarButtonEvents_ClickEventHandler(MenuItem_Click); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Create the procedure to remove the menu items when you close the document:
' Visual Basic Private Sub RemoveMenus() Dim MenuBarItem As Office.CommandBarPopup Try Dim MainMenuBar As Office.CommandBar = _ ThisApplication.CommandBars("Menu Bar") ' Need to locate the custom menu item again. MenuBarItem = DirectCast(MainMenuBar.FindControl( _ Office.MsoControlType.msoControlPopup, Tag:=MENU_TAG), _ Office.CommandBarPopup) If Not MenuBarItem Is Nothing Then MenuBarItem.Delete() End If Catch ' If there's an error, don't complain. End Try End Sub // C# private void RemoveMenus() { Office.CommandBarPopup MenuBarItem; try { Office.CommandBar MainMenuBar = ThisApplication.CommandBars["Menu Bar"]; // Need to locate the custom menu item again. MenuBarItem = (Office.CommandBarPopup)MainMenuBar.FindControl( Office.MsoControlType.msoControlPopup, Type.Missing, MENU_TAG, Type.Missing, Type.Missing); if (MenuBarItem != null) { MenuBarItem.Delete(true); } } catch { // If there's an error, don't complain. } }
Write code in the existing Open event handler for ThisDocument to initialize the menu items:
' Visual Basic Private Sub ThisDocument_Open() Handles ThisDocument.Open CreateMenus() End Sub // C# protected void ThisDocument_Open() { CreateMenus(); }
Write code in the existing Close event handler for ThisDocument to remove the menu item when you close the document:
' Visual Basic Private Sub ThisDocument_Close() Handles ThisDocument.Close RemoveMenus() End Sub // C# protected void ThisDocument_Close() { RemoveMenus(); }
Save your work and test by running the project.
You should be able to access the Load Spell Checker item from the Demo Code menu.
Conclusion
Accessing the Word spelling checker is easy, once you learn how to invoke the built-in collection of Dialog objects in Word. After you create your project by using Visual Studio Tools for the Microsoft Office System, you can write a procedure to invoke the spelling checker dialog box, and create a menu item to call your code.