The Right-Click Event

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

Differences Between Word, Excel, and PowerPoint

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.

Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click

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.

  1. In whichever application you're working with (Word, Excel, or PowerPoint), display the Visual Basic Editor and insert a new class module by clicking Class on the Insert menu. Add the following declaration.
  2. Public WithEvents App As Application
    

  3. Insert a new standard code module by clicking Module on the Insert menu. Add the following declaration and procedure to the top of the module:
  4. 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.

  5. After the InitEvents procedure, add the following procedure:
  6. 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.

  7. Place the cursor in the procedure InsertShortcut and press F5 to insert the custom shortcut menu.

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.

Word

The right-click event in Word triggers whenever you right click any part of a document in the active window.

Set Up the WindowBeforeRightClick Event in Word

Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."

  1. In the Visual Basic Editor started from Word, double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select WindowBeforeRightClick from the Procedures drop-down list in the class module. In the WindowBeforeRightClick event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowBeforeRightClick( _
        ByVal Sel As Selection, Cancel As Boolean)
        
        Application.CommandBars("NewShortcut") _
           .ShowPopup
        Cancel = True
    End Sub
    

  4. Double-click the standard code module project item, Module1, in the Project Explorer to make it the active window. Place the cursor in the procedure InitEvents and press F5 to run the project.
  5. Switch back to Word and right-click in the active document. You'll see the custom shortcut menu. You can extend the WindowBeforeRightClick event procedure to filter the content type in the selection beneath the right click and display shortcut menus based on the content.

Excel

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.

Set Up the SheetBeforeRightClick Event in Excel

Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."

  1. In the Visual Basic Editor started from Excel, double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select SheetBefore-RightClick from the Procedures drop-down list in the class module.
  3. 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
    

  4. Double-click the standard code module project item, Module1, in the Project Explorer to make it the active window. Place the cursor in the procedure InitEvents and press F5 to run the project.
  5. Switch back to Excel and right-click in the active document. You'll see the custom shortcut menu. You can extend the SheetBeforeRightClick event procedure to filter the content in the target range and display shortcut menus based on the content.

PowerPoint

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.

Set Up the WindowBeforeRightClick Event in PowerPoint

Before completing the following steps, complete the steps in the section "Display a Custom Shortcut Menu Based on the Selection Beneath a Right Click."

  1. In the Visual Basic Editor started from PowerPoint, double-click the Class1 project item in the Project Explorer to make it the active window.
  2. Click App from the Object drop-down list and then select WindowBeforeRightClick from the Procedures drop-down list in the class module. In the WindowBeforeRightClick event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowBeforeRightClick( _
        ByVal Sel As Selection, Cancel As Boolean)
    
        Application.CommandBars("NewShortcut") _
           .ShowPopup
        Cancel = True
    End Sub
    

  4. Double-click the standard code module project item, Module1, in the Project Explorer to make it the active window. Place the cursor in the procedure InitEvents and press F5 to run the project.
  5. Switch back to PowerPoint and right-click in the active document.
  6. 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.