The Double-Click Event

When you double-click content in a Word, Excel, or PowerPoint document, the action that follows depends on the content. For example, when you insert and double-click an ActiveX control, the Visual Basic Editor is displayed by default. Some developers want to write code for users so that instead of displaying the Visual Basic Editor, the default action is cancelled and a wizard or custom property dialog box appears.

In Word, Excel, and PowerPoint, the document's content below the double-click is viewed as a selection. When you double-click most parts of a document's content, you trigger the double-click event. The event occurs before the default double-click action that's built into the application. This action may be to display a dialog box, select text, or enter into text edit mode. This depends on whether the double click occurs in Word, Excel, or PowerPoint, and on the nature of the content that's double-clicked.

In all three applications, as the use of the word 'Before' in the name of the event implies, you can prevent (that is, cancel) the default behavior from occurring. As a result, you can customize the experience of double-clicking for any document content. The following chart shows the different names used for the double-click event procedure:

Office Application Double-click event procedure
Word WindowBeforeDoubleClick
Excel SheetBeforeDoubleClick
PowerPoint WindowBeforeDoubleClick

Differences Between Word, Excel, and PowerPoint

The main difference between the double-click events in Word, Excel, and PowerPoint is the argument 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 double click and perform a specific action. The selection may be text or a shape, for example. In Excel, three arguments are passed into the double-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 built-in default action.

Cancel the Default Action Based on the Selection Beneath a Double Click

You can perform many different actions when you double-click content. The actions, as previously mentioned, depend on the type of content that's double-clicked. Your double-click event procedures will normally contain a Select Case or an If…Then block that determines the type of content beneath the double click and performs a specific action. The following examples in Word, Excel, and PowerPoint show three different customizations of the double-click action.

  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 Module 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 name of the class module, you must also change the class name after the New keyword in the above declaration.

Now you're set up for either Word, Excel, or PowerPoint to customize the double-click action, based on the selection.

Word

The double-click event in Word triggers whenever you double-click any part of the document in the active window. Word takes different actions depending on the content you double-click. For example, when you double-click a shape like a rectangle, you see the Format AutoShape dialog box (as you do in Excel and PowerPoint). When you double-click a word, Word selects the entire word. If you insert a symbol (which is just a text character) through the Symbol dialog box, double-clicking the symbol results in the display of the Symbol dialog box. Since the WindowBeforeDoubleClick event procedure provides a Cancel argument, you can cancel any one of these built-in behaviors and customize the result of a double click.

When the Double-Click Event Triggers

When the Double-Click Event Does Not Trigger

Set Up the WindowBeforeDoubleClick Event in Word

When you click the Symbol menu item on the Insert menu, you see the Symbol dialog box. When you select a symbol, click the Insert button, click Close to dismiss the Symbol dialog box, and then double-click the symbol character in the document's text, you see the Symbol dialog box. The following steps show how to mimic the same type of functionality. Your code will determine if you clicked on a dollar sign, and if you did, it will display a message box. You can replace the call to display a message box with a call to show a userform.

Before completing the following steps, complete the steps in the section "Cancel the Default Action Based on the Selection Beneath a Double 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 WindowBefore-DoubleClick from the Procedures drop-down list in the class module.
  3. In the WindowBeforeDoubleClick event procedure, add the following code so that the procedure appears as follows:

    Private Sub App_WindowBeforeDoubleClick( _
        ByVal Sel As Selection, Cancel As Boolean)
        
        If Sel.Type = wdSelectionIP Then
            If Trim$(Sel.Text) = "$" Then
                Cancel = True
                Sel.Words(1).Select
                MsgBox "MySymbol"
            End If
        End If
    End Sub
    

    The If…Then statement evaluates the type of the selection that is passed into the WindowBeforeDoubleClick event procedure through the Sel argument. If the type of the selection is an insertion point (IP), a nested If…Then expression determines if the text of the selection is the dollar sign ($). In the evaluation, you use the built-in Visual Basic for Applications language function Trim$ to remove any spaces before and after the dollar sign. If the text under the double click is a dollar sign, the text is selected, as would normally happen during a double click, and the message box is displayed.

    You should note that when you double-click to select a word of text, the WindowBeforeDoubleClick event procedure is called just before Word expands the selection to the word. (Before you double-click, only an insertion point exists in the text.) If you cancel the default behavior, the word isn't selected; if you don't cancel, you get the built-in behavior of expanding the selection to the word. In this example, you can get the whole word where the insertion point exists. The code Sel.Words(1) returns the word that the insertion point is contained in and where the text is selected.

  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 type the dollar sign ($) in the active document.
  6. Double-click the dollar sign. You'll see the message box displaying the text "MySymbol." You can expand this solution to do anything, depending on what the selection is under the double click. As you'll see later in the PowerPoint example, you can prevent (cancel) the default dialog box from being displayed and display your own message or userform.

Excel

The double-click event in Excel fires when you double-click a cell in the active worksheet. If you double-click a cell that is not in edit mode, Excel's built-in behavior is to enter edit mode so that you can type text or a formula into the cell. The double-click event in Excel does not provide a way for you to determine if the user has double-clicked on a shape or any other object that floats on top of the cells in a worksheet.

Set Up the SheetBeforeDoubleClick Event in Excel

In the following example, you use the SheetBeforeDoubleClick event to prevent a cell from being edited when it's double-clicked; instead, the cell displays a message box. You can take this example and, as another option, display a custom userform that has a custom functions dialog box. Before completing the following steps, complete the steps in the section "Cancel the Default Action Based on the Selection Beneath a Double 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-DoubleClick from the Procedures drop-down list in the class module. In the SheetBeforeDoubleClick event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_SheetBeforeDoubleClick( _
        ByVal Sh As Object, ByVal Target As Range, _
        Cancel As Boolean)
        
        If Target.Address = "$A$1" Then
            Cancel = True
            MsgBox "Display custom function form"
        End If
    End Sub
    

    The If…Then statement evaluates the address of the cell beneath the double click. If the address of the cell is A1, the Cancel argument passed into the event procedure is set to True, thereby canceling the default behavior of entering the cell into edit mode. You then see the message box.

    Of course, this doesn't prevent the user from using the arrow keys on the keyboard to move the active cell to the A1 cell and pressing F2 to enter edit mode in the cell. In other scenarios, you can modify the double-click behavior on a cell to display a custom dialog box that, for example, allows the user to modify the formula of the cell.

  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 double-click the cell A1 on the active worksheet or any worksheet in the workbook.
  6. You won't be able to enter edit mode in cell A1 by double-clicking. The message box appears with the text, "Display custom function form." As previously mentioned, you can extend this example so that the SheetBeforeDoubleClick event procedure analyzes the cell formula, and if it's one that your code recognizes, a custom dialog box will be displayed. You can use the Formula property on the Range object to determine a cell's formula. The code in the evaluation of the If…Then statement above would start with Target.Formula = "=MyFunction(2+2)."

PowerPoint

The double-click event in PowerPoint triggers whenever you double-click a shape's border or a slide image in Slide Sorter view. As in Word, you can customize the action that takes place when the user double-clicks a specific shape type. For example, double-clicking an ActiveX control will by default display the Visual Basic Editor. Double-clicking an AutoShape displays the Format AutoShape dialog box. In any case, you can cancel the default action and display custom dialog boxes instead.

When the Double-Click Event Triggers

When you double-click a slide preview in PowerPoint Slide Sorter view, the selection type should be ppSelectionSlides. Note that when you double-click a slide preview in Slide Sorter view, PowerPoint will go to Normal view. However, you can cancel this action by setting the Cancel argument to True. You can double-click the slide preview in Notes Page view. In this case, the selection type is ppSelectionShape and the Cancel argument allows you to cancel the PowerPoint action that changes the view from Notes Page view to Normal view.

When the Double-Click Event Does Not Trigger

Set Up the WindowBeforeDoubleClick Event in PowerPoint

The following steps cancel the default action of displaying the Visual Basic Editor when you double-click an ActiveX control. Before completing the following steps, complete the steps in the section "Cancel the Default Action Based on the Selection Beneath a Double 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 WindowBefore-DoubleClick from the Procedures drop-down list in the class module. In the WindowBeforeDoubleClick event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowBeforeDoubleClick( _
        ByVal Sel As Selection, Cancel As Boolean)
    
        Dim shpSel As Shape
        If Sel.Type <> ppSelectionShapes Then Exit Sub
    
        Set shpSel = Sel.ShapeRange(1)
        If shpSel.Type = msoOLEControlObject Then
            Select Case TypeName(shpSel.OLEFormat.Object)
                Case "CommandButton"
                    MsgBox "CommandButton"
                    Cancel = True
                Case "Label"
                    MsgBox "Label"
                    Cancel = True
            End Select
        End If
    End Sub
    

    The first If…Then statement evaluates the selection type. If the selection isn't a shape, the WindowBeforeDoubleClick event procedure is exited. Otherwise the second If…Then statement determines the shape type. If it's an ActiveX control, the embedded Select Case block determines the type name of the control. If the control is a command button or label, a message box is displayed and the default action of displaying the Visual Basic Editor is canceled.

  4. Switch back to PowerPoint. Right-click a toolbar and from the shortcut menu click Control Toolbox to display the Control Toolbox toolbar.
  5. In the Control Toolbox toolbar, click and drag a command button, a label, and a list control onto the active slide. Insert some other drawing shapes.
  6. Switch to the Visual Basic Editor. 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.
  7. Switch to PowerPoint. Double-click the borders of the drawing shapes as well as the three controls.
  8. When you double-click the command button and label, you see a message box. When you double-click the list, you see the Visual Basic Editor, because that's the default action. When you double-click the border of a drawing shape, you see the Format AutoShape dialog box.