The following code may be used as a starting point for writing your own event handlers with regards to Outlook's MailItem Object.
This code will set an object reference to any MailItem you select in an active Explorer window. Place the following code into the ThisOutlookSession module.
Private WithEvents objExplorer As Outlook.Explorer
Private WithEvents msg As Outlook.MailItem
Private Sub Application_Startup()
Set objExplorer = Application.ActiveExplorer
End Sub
Private Sub objExplorer_SelectionChange()
If objExplorer.CurrentFolder.DefaultItemType = olMailItem Then
If objExplorer.Selection.Count > 0 Then
Set msg = objExplorer.Selection(1)
End If
End If
End Sub
This code declares two variables that will handle events. In the Startup event, we initialize it to point to the active Explorer (whichever one appears when Outlook starts).
The SelectionChange event allows us to grab whatever MailItem happens to be selected. That way, when we do something to it, our MailItem event handlers will fire.
Here are the event handlers for the MailItem Event. They should also be placed in the ThisOutlookSession module. Each one is intended simply to demonstrate how each one works and should be written. Once you place them (along with the code above) into Outlook's VB IDE, restart Outlook and then play around with e-mails to see when and how each event fires.
Private Sub msg_AttachmentAdd(ByVal Attachment As Attachment)
MsgBox "AttachmentAdd Event"
End Sub
Private Sub msg_AttachmentRead(ByVal Attachment As Attachment)
MsgBox "AttachmentRead Event"
End Sub
Private Sub msg_BeforeAttachmentSave(ByVal Attachment As Attachment, Cancel As Boolean)
MsgBox "BeforeAttachmentSave Event"
End Sub
Private Sub msg_BeforeCheckNames(Cancel As Boolean)
MsgBox "BeforeCheckNames Event"
End Sub
Private Sub msg_BeforeDelete(ByVal Item As Object, Cancel As Boolean)
MsgBox "BeforeDelete Event"
End Sub
Private Sub msg_Close(Cancel As Boolean)
MsgBox "Close Event"
End Sub
Private Sub msg_CustomAction(ByVal Action As Object, ByVal Response As Object, _
Cancel As Boolean)
MsgBox "CustomAction Event"
End Sub
Private Sub msg_CustomPropertyChange(ByVal Name As String)
MsgBox "CustomPropertyChange Event"
End Sub
Private Sub msg_Forward(ByVal Forward As Object, Cancel As Boolean)
MsgBox "Forward Event"
End Sub
Private Sub msg_Open(Cancel As Boolean)
MsgBox "Open Event"
End Sub
Private Sub msg_PropertyChange(ByVal Name As String)
MsgBox "PropertyChange Event"
End Sub
Private Sub msg_Read()
MsgBox "Read Event"
End Sub
Private Sub msg_Reply(ByVal Response As Object, Cancel As Boolean)
MsgBox "Reply Event"
End Sub
Private Sub msg_ReplyAll(ByVal Response As Object, Cancel As Boolean)
MsgBox "ReplyAll Event"
End Sub
Private Sub msg_Send(Cancel As Boolean)
MsgBox "Send Event"
End Sub
Private Sub msg_Write(Cancel As Boolean)
MsgBox "Write Event"
End Sub
