Overriding BuiltIn Commands

It is easy to override a built-in Word command using VBA. To do so, you create a subroutine that has the same name as the built-in command. Each time the user clicks the button for that command on the toolbar or menu or presses a keyboard shortcut, your custom subroutine runs instead of Word's built-in command.

You can't do this in VSTO because of limitations in the Word and Excel architecture. One thing you can do is to create a callback from VBA into your managed code within VSTO. You can use VSTO to replace the built-in button or menu item with your own. Note that you still need to create a callback in VBA to call into your managed code and override Word's built-in keyboard shortcut. You can see an example of how to write a callback for an Excel user-defined function in Chapter 7.

Let's say that you want to replace Word's built-in spell-checking functionality because you have three additional requirements. First, you want to ensure that the spell-check occurs every time the Print button is clicked. (Word's default functionality is to rerun the spell-checker only on new text in the document that hasn't already been checked.)

Second, you want to make sure that the document is saved as soon as the spell-check operation is complete. You can do this by adding a method to your project to ensure that the document will always be rechecked for spelling errors and then calling the Save method on the document after the spell-checking is complete. You can create and add your own button or menu item that is identical to the built-in item; then you hide the built-in item.

Finally, you decide that you don't want to check grammar in the document no matter what options the user has set.

The first step is to create the menu item. Add the code in Listing 6.4 to the ThisApplication class before the code for the Startup method.

Listing 6.4. Creating a toolbar button to override Word's spell-checking command

Dim MySpellCheckButton As Office.CommandBarButton Dim MySpellCheckMenuItem As Office.CommandBarButton Dim ToolsMenu As Office.CommandBar = _

Application.CommandBars("Tools") Dim StandardToolbar As Office.CommandBar = _

Application.CommandBars("Standard") Const CONTROL_NAME As String = "&Spelling and Grammar..." Const CAPTION_TEXT As String = "&Spelling..." Const MENU_TAG As String = "SpellCheck" Const BUTTON_TAG As String = "CheckSpelling"

Private Sub AddMenuItem()

' If the menu item already exists, remove it, and show ' the built-in item.

MySpellCheckMenuItem = ToolsMenu.FindControl( _ Tag:=MENU_TAG)

If MySpellCheckMenuItem IsNot Nothing Then MySpellCheckMenuItem.Delete(True) ToolsMenu.Controls(CONTROL_NAME).Visible = True End If

Catch Ex As Exception


End Try

' Set the customization context to the document. Me.Application.CustomizationContext = Me.InnerObject

' Hide the default Spelling and Grammar control. ToolsMenu.Controls(CONTROL_NAME).Visible = False

' Add the new Spelling item.

MySpellCheckMenuItem = CType(ToolsMenu.Controls.Add( _ Type:=Office.MsoControlType.msoControlButton, _ Before:=1, Temporary:=True), _ Office.CommandBarButton)

With MySpellCheckMenuItem

.Caption = CAPTION_TEXT .Tag = MENU_TAG .FaceId = 2 End With

AddHandler MySpellCheckMenuItem.Click, _ AddressOf SpellCheck

End Sub

Next, create the toolbar button, and add it to Word's Standard toolbar by adding the code in Listing 6.5 following the AddMenuItem method.

Listing 6.5. Creating a menu item to override Word's spell-checking command Private Sub AddToolbarButton()

' If the button already exists, remove it, and show ' the built-in button.

MySpellCheckButton = StandardToolbar.FindControl( _ Tag:=BUTTON_TAG)

If MySpellCheckButton IsNot Nothing Then MySpellCheckButton.Delete(True)

StandardToolbar.Controls(CONTROL_NAME).Visible _ = True

End If Catch Ex As Exception MsgBox(Ex.Message) End Try

' Set the customization context to the document. Me.Application.CustomizationContext = Me.InnerObject

' Hide the default Spelling and Grammar control. StandardToolbar.Controls(CONTROL_NAME).Visible = False

' Add the new Spelling control.

MySpellCheckButton = CType(StandardToolbar.Controls _ .Add(Type:=1, Before:=8), Office.CommandBarButton)

With MySpellCheckButton

.Caption = CAPTION_TEXT .Tag = BUTTON_TAG .FaceId = 2 End With

AddHandler MySpellCheckButton.Click, _ AddressOf SpellCheck

Add the SpellCheck event handler, and add code to the Startup event handler that calls the AddMenuItem and AddToolbarButton methods, as shown in Listing 6.6.

Listing 6.6. Creating the event handler and calling methods to create menu and toolbar buttons

Private Sub SpellCheck(ByVal ctrl As Office.CommandBarButton, _ ByRef Cancel As Boolean)

' Reset the spell-check options, run spell-check, and ' then save the document. Application.ResetIgnoreAll() Me.SpellingChecked = False Me.CheckSpelling() Me.Save()

MsgBox("The spelling check is complete.", _

MsgBoxStyle.OkOnly, "Microsoft Office Word")

End Sub

Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' Create the menu item and toolbar button. AddMenuItem() AddToolbarButton()

End Sub

This code checks for the existence of our custom menu item or toolbar button, and if it finds the item, it deletes it and then re-creates it. In this way, we don't repeatedly add the same button to the toolbar or menu each time the document is opened. The code then hides the built-in commandbar items and creates a Click event handler for both the toolbar button and the menu item. In the event handler, spell-check options are reset so that the entire document will be checked again; the

CheckSpelling method of the document is called, and then changes are saved to the document. To mimic the default functionality, we display a message box to inform users that the document has been spell-checked. This is a necessary visual cue that something actually happened when the user clicked the Spelling button even if no errors occurred in the document.

Creating menu items and buttons is important because it is often the entry point into your code. The Word object model contains many objects that you can manipulate from VSTO, but this chapter does not focus on the existing object model; you can learn more about it by using the Object Browser and IntelliSense within Visual Studio. As mentioned before, you can also learn about Word's object model by using the macro recorder.

VSTO has extended some of the objects in the Word object model. In the remainder of this chapter, we take a closer look at these enhancements and describe how you can use these objects in your Word solutions using VSTO.

0 0

Post a comment