问题
I have the following code:
Option Explicit
Public WithEvents myOlExp As Outlook.Explorer
' Initiation
Private Sub Application_Startup()
Set myOlExp = Application.ActiveExplorer
End Sub
' Termination
Private Sub myOlExp_Close()
MsgBox "quit"
End Sub
The code itself works fine - with one important limitation (= my problem). The Explorer_Close event is only triggered when multiple Outlook windows are open and these are being closed. However when the last/main Outlook window is being closed, then this event is not triggered.
I have also tried Application_Quit - but when this runs it is no longer possible to process mail items.
Background: I am trying to reorganize certain items when I close Outlook.
回答1:
You must subscribe to each Explorer
instance individually and keep these references alive if you want to get events fired each time.
Basically, you need to maintain the list of Explorer
objects opened by handling the NewExplorer event which is fired whenever a new explorer window is opened, either as a result of user action or through program code. In the event handler, you may add a new Explorer
instance to the collection and remove it in the Close event handler when a corresponding instance is closed.
来源:https://stackoverflow.com/questions/59224587/ms-outlook-explorer-close-event