Barbara said:
When I use this event, there is *no* change to the windows arrangement,
because the code runs *before* the workbook window is closed. As such,
the arrangement doesn't changet. I want to automatically arrange the
remaining windows after the workbook is closed.
Since the App_WorkbookBeforeClose passes the closing workbook as an
argument, you could use
Private Sub MyWindow_WorkbookBeforeClose( _
ByVal Wb As Excel.Workbook, Cancel As Boolean)
Dim wkBk As Workbook
For Each wkBk In Application.Workbooks
If wkBk.Name <> Wb.Name Then
'arrange
End If
Next wkBk
End Sub
The Deactivate event is the one that gave me trouble, exactly for the
reasons given. I'm a newbie at Visual Basic, so setting a flag is
something I haven't done.
Something like this is a bit of a kludge, but it may work for you. The
flag bClosing is set by the App_WorkbookBeforeClose event macro. When
the workbook is deactivated, that flag is checked to see if it's the
result of a workbook closing or just switching workbooks. The
Application.OnTime code is added in the case that XL asks the user
whether to save and the user clicks "Cancel" - it clears the bClosing
flag after 15 seconds. It's not a perfect solution, as if the user
switches workbooks after canceling a save, App_WorkbookDeactivate will
treat it as a close event. You can change the timing to make that less
likely, but if the user then delays in saving, the arrangement might not
happen:
In the MyWindow (or whatever you name it) class module:
Private Sub MyWindow_WorkbookActivate(ByVal Wb As Excel.Workbook)
bClosing = False
End Sub
Private Sub MyWindow_WorkbookBeforeClose( _
ByVal Wb As Excel.Workbook, Cancel As Boolean)
bClosing = True
Application.OnTime Now + TimeSerial(0, 0, 15), "ClearFlag"
End Sub
Private Sub MyWindow_WorkbookDeactivate(ByVal Wb As Excel.Workbook)
Dim wkBk As Workbook
If bClosing Then
For Each wkBk In Application.Workbooks
If wkBk.Name <> Wb.Name Then
'arrange
End If
Next wkBk
End If
End Sub
in a regular code module:
Public bClosing As Boolean
Public Sub ClearFlag()
bClosing = False
End Sub
Why isn't there an App_WorkbookAfterClose event?
Because MS hasn't exposed it?