An OnWindow event handler runs whenever the user switches to a window. To associate a procedure with switching to any window in the Microsoft Excel application, use the OnWindow property of the Application object, as shown in the following example.
Application.OnWindow = "AllWindowHandler"
To associate a procedure with switching to any window in the Microsoft Excel application, use the OnWindow property of a Window object. To associate a procedure with switching to a specific sheet in a workbook, use an Auto_Activate procedure, as described earlier in this chapter.
For example, the following code contains a procedure that traps each instance of switching to the second window called BikeDB.xls and then forces the window to appear at a specific location on-screen.
Sub TrapBicycle2()
Windows("BikeDB.XLS:2").OnWindow = "PositionWindow"
End Sub
Sub PositionWindow()
With Windows("BikeDB.XLS:2")
.Left = 0
.Top = 100
.Width = 300
.Height = 50
End With
End Sub
If a workbook, worksheet, or other sheet has an associated OnSheetActivate procedure, it runs after the procedure specified in the OnWindow property.
Note
The OnWindow event handler doesn't run if you switch to a window under program control.