oliv-
Senior Member
- Outlook version
- Outlook 2010 32 bit
- Email Account
- Exchange Server
Hi,
What is the best pratice for catching Mailitem events ? (response, forward, etc...)
i saw in this article : VBA Sample: Do Something When Reply is Clicked
this code
And I use this one :
ThisOutlookSession
Class module
What is the best pratice for catching Mailitem events ? (response, forward, etc...)
i saw in this article : VBA Sample: Do Something When Reply is Clicked
this code
Code:
Option Explicit
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
Private Sub Application_Startup()
Set oExpl = Application.ActiveExplorer
bDiscardEvents = False
End Sub
Private Sub oExpl_SelectionChange()
On Error Resume Next
Set oItem = oExpl.Selection.Item(1)
End Sub
' Reply
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
Cancel = True
bDiscardEvents = True
Set oResponse = oItem.Reply
afterReply
End Sub
Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
Cancel = True
bDiscardEvents = True
Set oResponse = oItem.Forward
afterReply
End Sub
And I use this one :
ThisOutlookSession
Code:
Private m_MyEmails As VBA.Collection
Private m_lNextKeyEmails As Long
Private Sub Application_ItemLoad(ByVal item As Object)
'---------------------------------------------------------------------------------------
' Procedure : Application_ItemLoad
' Author : Oliv
' Date : 07/03/2016
' Purpose : EMAIL WRAPPER / Receive Events of Multiple Emails
' BASED ON : http://www.vboffice.net/en/developers/inspector-wrapper-receive-events-of-multiple-emails/
'---------------------------------------------------------------------------------------
'
Dim oMail As cMail
If m_MyEmails Is Nothing Then Set m_MyEmails = New VBA.Collection
Set oMail = New cMail
If item.Class = olMail Then
If oMail.Init(item, CStr(m_lNextKeyEmails)) Then
m_MyEmails.Add oMail, CStr(m_lNextKeyEmails)
m_lNextKeyEmails = m_lNextKeyEmails + 1
End If
End If
End Sub
Friend Property Get MyEmails() As VBA.Collection
Set MyEmails = m_MyEmails
End Property
Class module
Code:
'---------------------------------------------------------------------------------------
' Module : cMail
' Author : Oliv
' Date : 07/03/2016 17:49
' Purpose : EMAIL WRAPPER / Receive Events of Multiple Emails
'---------------------------------------------------------------------------------------
Private WithEvents m_Mail As Outlook.MailItem
Private m_IsClosed As Boolean
Private m_sKey As String
Friend Function Init(oEmail As Outlook.MailItem, sKey As String) As Boolean
Dim obj As Object
If Not oEmail Is Nothing Then
Set m_Mail = oEmail
m_sKey = sKey
Init = True
End If
End Function
Private Sub Class_Terminate()
CloseEmail
End Sub
Friend Sub CloseEmail()
On Error Resume Next
If m_IsClosed = False Then
m_IsClosed = True
ThisOutlookSession.MyEmails.Remove m_sKey
Set m_Mail = Nothing
End If
End Sub
Private Sub m_Mail_Close(Cancel As Boolean)
CloseEmail
End Sub
Private Sub m_Mail_Forward(ByVal Forward As Object, Cancel As Boolean)
If Forward.Class = olMail And OptResponseHTML Then
MsgBox "fire m_Mail_Forward" & vbCr & Forward.BodyFormat
End If
End Sub