Microsoft Windows 95 introduced the widespread use of right-clicking. You can right-click almost anything in Windows and most applications. When you right-click, you see a shortcut menu containing a list of items to select. Usually, the list contains the most common actions that pertain to the object you right-clicked. When you right-click content in a Word, Excel, or PowerPoint document, you do see a shortcut menu; what items it lists depends on the content below the right click.
The document's content below the right click is viewed as a selection in Word, Excel, and PowerPoint. You should note that the right-click event occurs only when any part of a document's content is right-clicked. It doesn't happen when you right-click a command bar. In Word, Excel, and PowerPoint, the right-click event occurs before the default right-click action, which is to display a built-in shortcut menu.
In all three applications, as the use of the word "Before" in the name of the event implies, you can cancel the display of the default shortcut menu and, therefore, you can display your own custom shortcut menu for any document content. The chart below shows the different names used for the right-click event procedure.
Office Application | Right-click event procedure |
---|---|
Word | WindowBeforeRightClick |
Excel | SheetBeforeRightClick |
PowerPoint | WindowBeforeRightClick |
The main difference between the right-click events in Word, Excel, and PowerPoint is the argument that's passed into the event procedure. In Word and PowerPoint, two arguments—an instance of the Selection object and the Cancel Boolean—are passed to the event procedure in each application. Using the Selection object, you can determine what type of object is below the right click and display a specific custom shortcut. The selection may be text or a shape, for example. In Excel, three arguments are passed into the right-click event. The second argument is always a Range object.
The second argument in Word and PowerPoint and the third argument in Excel is the Cancel Boolean. You can set the Cancel Boolean to True if you want to cancel the display of the built-in, default shortcut menu. In the following example, a custom shortcut menu is displayed and the display of the built-in shortcut menu is canceled.
The most common scenario that the right-click event allows you to handle is the display of a custom shortcut menu based on the content that the user right-clicked. In the following sections, you'll create a custom shortcut menu in Word, Excel, and PowerPoint. The custom shortcut menu that's displayed will be based on the type of the current selection.
For each procedure under the descriptions of the right-click event in Word, Excel, and PowerPoint, you need to complete the following steps first. The code in the following steps is generic, because it uses the CommandBar object model and, hence, works identically in Word, Excel, and PowerPoint. Note that if you started with the steps from the sections "The Selection Change Event" or "The Double-Click Event," you've most likely completed the first two steps already.
Public WithEvents App As Application |
Dim AppEvents As New Class1 Sub InitEvents() Set AppEvents.App = Application End Sub |
The class name "Class1" stated just after the New keyword in the declaration statement should be the name of the class module you added in the previous step. If you've changed the class module's name, you must also change the class name after the New keyword in the above declaration.
Sub InsertShortcut() Dim cmbNewShortcut As CommandBar Set cmbNewShortcut = Application _ .CommandBars.Add(Name:="NewShortcut", _ Position:=msoBarPopup, Temporary:=True) With cmbNewShortcut.Controls With .Add .Caption = "&ShortcutItem 1" End With With .Add .Caption = "&ShortcutItem 2" End With End With End Sub |
This procedure is copied from the procedure InsertShortcutMenu from the "Add a Shortcut Menu" section in the next chapter. The procedure adds a custom shortcut menu named NewShortcut to the command bar collection. The custom shortcut menu contains two menu items. You can use this procedure in all applications.
In the Add method of the CommandBars collection object, you specify the Position and the Temporary arguments. As discussed in the next chapter, which describes working with the CommandBar objects, methods and properties, you need to specify the Position argument in order to create a shortcut menu. Setting the Temporary argument to True indicates that the shortcut menu will be removed when the Office application is exited.
Now you're set up for either Word, Excel, or PowerPoint to display a custom shortcut menu, based on the selection. Once you complete the steps under the following Word, Excel, and PowerPoint sections, a right click will result in the display of a custom shortcut menu rather than the application's built-in, default shortcut menu.
The right-click event in Word triggers whenever you right click any part of a document in the active window.
Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."
Private Sub App_WindowBeforeRightClick( _ ByVal Sel As Selection, Cancel As Boolean) Application.CommandBars("NewShortcut") _ .ShowPopup Cancel = True End Sub |
The right-click event in Excel fires when you right-click a range of cells on the active worksheet. In most cases, the range contains only one cell. The right-click event in Excel does not provide a way for you to determine if the user has right-clicked a shape or any other object that floats on top of the cells in a worksheet.
Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."
In the SheetBeforeRightClick event procedure, add the following code so that the procedure appears as follows:
Private Sub App_SheetBeforeRightClick( _ ByVal Sh As Object, ByVal Target As Range, _ Cancel As Boolean) Application.CommandBars("NewShortcut") _ .ShowPopup Cancel = True End Sub |
The right-click event procedure is called when you right-click a shape, text, or a slide in any view, including a master view and Notes Page view. This event is triggered after the right mouse button returns to the up position after being pressed down. The cancel behavior prevents (cancels) only the context menu from appearing.
Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."
Private Sub App_WindowBeforeRightClick( _ ByVal Sel As Selection, Cancel As Boolean) Application.CommandBars("NewShortcut") _ .ShowPopup Cancel = True End Sub |
You'll see the custom shortcut menu. You can extend the Window-BeforeRightClick event procedure to filter the content type in the selection beneath the right click and display shortcut menus based on the content.