The Window Activate and Deactivate Events

When you switch between windows in Windows, one window becomes the active window while the window you're moving away from becomes inactive. You can see the difference by the change in color of the window's title bar. When you switch between documents in Word, Excel, and PowerPoint, the same behavior occurs. Knowing when a document window becomes active or inactive allows you to develop solutions that address scenarios like the following:

Differences Between Word, Excel, and PowerPoint

The names of the window activate and deactivate events are the same in Word, Excel, and PowerPoint: WindowActivate and WindowDeactivate. Like the other events described in this chapter, they apply at the application level, so you can determine when any document window becomes active or inactive. In all three applications, two arguments are passed to each event. The first is the document in the window that's being activated or deactivated, and the second is the document window.

The main difference between Word, Excel, and PowerPoint is that in Word and PowerPoint, when you navigate to and from a window that doesn't belong to the application, the window activate and deactivate events will be triggered for the application's document windows. In Excel, the window activate and deactivate events will be triggered only when you navigate between Excel document windows.

If you navigate from an Excel document window to Notepad, for example, the window deactivate event doesn't get triggered. Conversely, navigating from Notepad to an Excel document window doesn't trigger the window activate event. When you navigate to and from Notepad to a document window in Word or PowerPoint, however, the window activate and deactivate events trigger each time a document window becomes active and inactive respectively.

Word

When you switch between document windows in Word, the selection change event doesn't get triggered. Since each window in Word maintains its own selection, as in PowerPoint, the selection doesn't change when you switch between document windows. Thus, you use the window activate event to determine when the active document window has changed and, therefore, to determine what the active selection is in order to update any command bar customizations. In the following steps, the selection change event procedure is explicitly called from the window activate event so that the same code in the selection change event procedure doesn't have to be repeated in the window activate event. This example continues the scenario of updating custom command bar controls that was started with the selection change event procedure at the beginning of this chapter.

When the Window Activate and Deactivate Events Trigger

When the Window Activate and Deactivate Events Do Not Trigger

Set Up the WindowActivate Events in Word

Before completing the following steps, complete the steps in the section "Update Your Controls Based on a Selection Change" and the Word-specific steps just after that section.

  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 WindowActivate from the Procedures drop-down list in the class module. In the WindowActivate event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowActivate( _
        ByVal Doc As Document, ByVal Wn As Window)
        
        App_WindowSelectionChange Wn.Selection
    End Sub
    

    The window selection change event in Word takes one argument, the Selection object. In the WindowActivate event procedure, the Wn argument represents the active window. You use the Selection property on the Wn object to return the active selection object, which is passed to the WindowSelectionChange event procedure.

  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, add some text to the document, add shapes, and change the style of some of the text to bold.
  6. Click the document's content, such as the bold text, not-bold (lightface) text, or a shape.
  7. Create a new document and insert some content along with more text. Format some text to bold.
  8. Switch between the document windows. The selection is maintained between windows, but the WindowActivate event is called when you switch between windows, and the custom Bold button still toggles between the up and down (depressed) state and between enabled and disabled.

Excel

When you switch between document windows in Excel, the selection change event doesn't get triggered. Because in Excel each sheet in the window maintains its own selection, the selection doesn't change when you switch between document windows and sheets within a window. Thus, you use the window activate event and the sheet activate event to determine when the active document window and sheet have changed. With the combination of these two events, you can determine what the active selection is in order to update any command bar customizations. In the following steps, the selection change is explicitly called from the window activate and sheet activate events, so that the same code in the selection change event procedure doesn't have to be repeated in other event procedures.

When the Window Activate and Deactivate Events Trigger

When the Window Activate and Deactivate Events Do Not Trigger

Set Up the WindowActivate Events in Excel

Before completing the following steps, complete the steps in the section "Update Your Controls Based on a Selection Change" and the Excel-specific steps just after that section.

  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 WindowActivate from the Procedures drop-down list in the class module. In the WindowActivate event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowActivate( _
        ByVal Wb As Workbook, ByVal Wn As Window)
        
        App_SheetSelectionChange _
            Wn.ActiveSheet, Wn.Selection
    End Sub
    

    The window selection change event in Excel takes two arguments, the active sheet in the activated window and the Range object. In the WindowActivate event procedure, the Wn argument represents the active window. You use the Selection property on the Wn object to return the object in the active selection, which is passed to the SheetSelectionChange event procedure.

  4. From the Procedures drop-down list in the class module, click SheetActivate and add the following code to the event procedure so that the procedure appears as follows:
  5. Private Sub App_SheetActivate(ByVal Sh As Object)
        If TypeName(Sh) = "Worksheet" Then
        App_SheetSelectionChange _
            Sh, ActiveWindow.Selection
        End If
    End Sub
    

    In the SheetActivate event procedure, the Sh argument represents the active sheet in the activated window.

  6. 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 back to Excel, add some text to a cell, add shapes, and format the text to bold in any cell that contains text.
  8. Click the workbook's content, such as the bold text, not-bold (lightface) text, or a shape.
  9. Create a new workbook, and insert some content, along with more text. Format some text to bold.
  10. Switch between the document windows and sheets within a window. The selection is maintained between windows and between sheets. However, the WindowActivate and the SheetActivate events are called when you switch between windows and sheets, respectively. The custom Bold button therefore toggles between the up and down (depressed) state.

PowerPoint

When you switch between document windows in PowerPoint, the selection change event procedure doesn't get triggered. Because each window in PowerPoint maintains its own selection, as in Word, the selection doesn't change when you switch between document windows. Thus, you use the window activate event procedure to determine when the active document window has changed and, therefore, to determine what the active selection is in order to update any command bar customizations. In the following steps, the selection change is explicitly called from the window activate event procedure so that the same code in the selection change event procedure doesn't have to be repeated in the window activate event procedure.

When the Window Activate and Deactivate Events Trigger

When the Window Activate and Deactivate Events Do Not Trigger

Set Up the WindowActivate Events in PowerPoint

Before completing the following steps, complete the steps in the section "Update Your Controls Based on a Selection Change" and the PowerPoint-specific steps just after that section.

  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 WindowActivate from the Procedures drop-down list in the class module. In the WindowActivate event procedure, add the following code so that the procedure appears as follows:
  3. Private Sub App_WindowActivate( _
        ByVal Pres As Presentation, _
        ByVal Wn As DocumentWindow)
    
        On Error GoTo Error_Handler
        App_WindowSelectionChange Wn.Selection
    
    Error_Handler:
        Debug.Print Err.Description
    End Sub
    

    The window selection change event in PowerPoint takes one argument, the Selection object. In the WindowActivate event procedure, the Wn argument represents the active window. You use the Selection property on the Wn object to return the active selection object, which is passed to the WindowSelectionChange event procedure.

  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, add some text to a cell, add shapes, and format the text to bold in any cell that contains text.
  6. Create a new presentation and insert some content along with more text. Format some text to bold.
  7. Switch between the document windows.
  8. The selection is maintained between windows. However, the WindowActivate event is called when you switch between windows and the custom Bold button still toggles between the up and down (depressed) state and between enabled and disabled.