问题
Does any one know if it is possible to hook into the event when a user changes the 'From' address drop-down in Outlook 2016:
I've gotten as far as testing out some VBA macros for events Application.ItemLoad
and Application.ItemSend
etc. but I was hoping there was more events I could hook into.
回答1:
Of course - MailItem.PropertyChange
event will fire:
PropertyChange ("SendUsingAccount")
PropertyChange ("SentOnBehalfOfName")
You can see the live events in OutlookSpy - open the new item, click Item button on the OutlookSpy ribbon, go to the Events tab - OutlookSpy will log the events as they are raised.
回答2:
The PropertyChange event of the MailItem class is fired when an explicit built-in property (for example, Subject) of an instance is changed. Be aware, you may not get the event fired as soon as you change the value in the UI. Outlook may not fire events until the focus is moved to another field or the item is saved.
回答3:
For completeness - here is the full code I have implemented to capture the event I am interested in.
Dim WithEvents myInspector As Outlook.Inspectors
Dim WithEvents myMailItem As Outlook.MailItem
Private Sub Application_Startup()
Set myInspector = Application.Inspectors
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Outlook.Inspector)
If TypeOf Inspector.CurrentItem Is MailItem Then
Set myMailItem = Inspector.CurrentItem
End If
End Sub
Private Sub myMailItem_PropertyChange(ByVal Name As String)
' Properties we are interested in: "SendUsingAccount" / "SentOnBehalfOfName"
' Both get fired when the 'From' field is changed/re-selected
' So we are only going to trigger on one event or we will call the code twice
If Name = "SentOnBehalfOfName" Then
MsgBox myMailItem.SentOnBehalfOfName
End If
End Sub
来源:https://stackoverflow.com/questions/42654337/outlook-event-on-change-from-address