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:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.