The following code may be used as a starting point for writing your own event handlers with regards to Outlook's PostItem Object.
This code will set an object reference to any PostItem you select in an active Explorer window. Place the following code into the ThisOutlookSession module.
Private WithEvents objExplorer As Outlook.Explorer
Private WithEvents post As Outlook.PostItem
Private Sub Application_Startup()
Set objExplorer = Application.ActiveExplorer
End Sub
Private Sub objExplorer_SelectionChange()
If objExplorer.CurrentFolder.DefaultItemType = olPostItem Then
If objExplorer.Selection.Count > 0 Then
Set post = objExplorer.Selection(1)
End If
End If
End SubThis code declares two variables that will handle events. In the Startup event, we merely initialize it to point to the active Explorer (whichever one appears when Outlook starts). This is only to get our foot in the door.
The SelectionChange event allows us to grab whatever PostItem happens to be selected. That way, when we do something to it, our PostItem event handlers will fire.
Here are the event handlers for the PostItem 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 Posts to see when and how each event fires.
Private Sub post_AttachmentAdd(ByVal Attachment As Attachment) MsgBox "AttachmentAdd Event" End Sub Private Sub post_AttachmentRead(ByVal Attachment As Attachment) MsgBox "AttachmentRead Event" End Sub Private Sub post_BeforeAttachmentSave(ByVal Attachment As Attachment, Cancel As Boolean) MsgBox "BeforeAttachmentSave Event" End Sub Private Sub post_BeforeCheckNames(Cancel As Boolean) MsgBox "BeforeCheckNames Event" End Sub Private Sub post_BeforeDelete(ByVal Item As Object, Cancel As Boolean) MsgBox "BeforeDelete Event" End Sub Private Sub post_Close(Cancel As Boolean) MsgBox "Close Event" End Sub Private Sub post_CustomAction(ByVal Action As Object, ByVal Response As Object, Cancel As Boolean) MsgBox "CustomAction Event" End Sub Private Sub post_CustomPropertyChange(ByVal Name As String) MsgBox "CustomPropertyChange Event" End Sub Private Sub post_Forward(ByVal Forward As Object, Cancel As Boolean) MsgBox "Forward Event" End Sub Private Sub post_Open(Cancel As Boolean) MsgBox "Open Event" End Sub Private Sub post_PropertyChange(ByVal Name As String) MsgBox "PropertyChange Event" End Sub Private Sub post_Read() MsgBox "Read Event" End Sub Private Sub post_Reply(ByVal Response As Object, Cancel As Boolean) MsgBox "Reply Event" End Sub Private Sub post_ReplyAll(ByVal Response As Object, Cancel As Boolean) MsgBox "ReplyAll Event" End Sub Private Sub post_Send(Cancel As Boolean) MsgBox "Send Event" End Sub Private Sub post_Write(Cancel As Boolean) MsgBox "Write Event" End Sub
