Before RightClick

The BeforeRightClick event enables you to take action when a user right-clicks text within a bookmark, before the default action for the right-click event occurs. A common example is to display a context menu. You can create your own context menu, or you can add a menu item to the default menu that displays when you right-click a document. In this section you'll create a solution to demonstrate the BeforeRightClick event of a bookmark named Bookmark1.

1. Create a Word document solution.

2. Add a bookmark to text within the document, and name it Bookmark1.

3. Add the code in Listing 6.21 to the top of the ThisDocument class file. This code sets up the variable for the CommandBar button, a CommandBar control, and the string to be assigned to the button's caption. You can assign any string value to the controlCaption variable.

Listing 6.21. Creating variables for a context menu button

WithEvents newControl As Office.CommandBarButton

Dim myControl As Office.CommandBarControl

Dim controlCaption As String = "New Menu Item"

4. Add the code in Listing 6.22 to the BeforeRightClick event handler of the Bookmark.

Listing 6.22. Adding a control to a context menu

Private Sub Bookmark1_BeforeRightClick(ByVal sender As _

System.Object, ByVal e As Microsoft.Office.Tools.Word. _ ClickEventArgs) Handles Bookmark1.BeforeRightClick

For Each myControl In Application.CommandBars( _ "Text").Controls

If myControl.Caption = controlCaption Then

Application.CommandBars("Text").Controls( _ controlCaption).Delete()

End If

Next newControl = Application.CommandBars("Text").Controls.Add With newControl

.BeginGroup = True

.Style = Office.MsoButtonStyle.msoButtonIconAndCaption .Caption = controlCaption .FaceId = 1763 .Tag = controlCaption End With

Me.AttachedTemplate.Saved = True End Sub

The code first loops through all the controls on Word's context menu and checks whether the button was added previously. If it was, the code removes the button before attempting to add another one. Without this code, each time you right-click the bookmark, another button would be added to the context menu.

Next, you add a new control to the Text CommandBar, Word's default context menu. You set various properties to ensure that the button is added to a new group within the menu, to ensure that it can contain both an icon and text, to assign a string to the caption, and to assign a

FaceID for the button. The FaceID property identifies a built-in icon to use. The tag property should be set to a unique value so that Word can determine which button on the context menu was clicked. In this case, you use the same name as the control caption, but instead you could have assigned it a unique value by using a GUID. Next, you need to add a handler for the event.

5. Add the code in Listing 6.23 to the ThisDocument class file.

Listing 6.23. Creating an event handler for a shortcut menu button

Private Sub ShortcutButtonClick(ByVal ctrl As _

Office.CommandBarButton, ByRef Cancel As Boolean) _ Handles newControl.Click

MsgBox(ctrl.Caption & " was clicked.")

End Sub

6. Press F5 to run the code.

When you right-click the bookmark, the event handler adds the button to the context menu, and the menu item becomes available as the final entry in the shortcut menu. When you click the button, a message box displays the name of the clicked control. You can expand this code to handle the right-click event of all bookmarks in the document; you loop through all the bookmarks in the document via the Controls collection (as described earlier in this chapter) and add an event handler for each bookmark.

Because bookmarks can overlap, you should write additional code to ensure that you are handling the event of the correct bookmark. Overlapping bookmarks are described in more detail later in this chapter.

0 0

Post a comment