Best practice for catching mailitem.events

Status
Not open for further replies.

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

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
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Horsepower What is best calendar practice for iMac Using Outlook 3
cheapfaremart Which outlook version is best? Using Outlook 0
J What is the best EntryID format to set on MS Access table Outlook VBA and Custom Forms 3
B What is best IMAP .OST file to .PST file converter solutions? Using Outlook 1
P Best configuration for Outlook 2016, Gmail and iCloud Using Outlook 8
P Outlook room resource calendars and best practices Exchange Server Administration 0
Rupert Dragwater What is the best way to back up pst Using Outlook 4
Diane Poremsky The Best Outlook Store Add-ins Contest Using Outlook 0
Patrick van Berkel Best way to share (and keep up-to-date) Macro's in Outlook 2010 Outlook VBA and Custom Forms 6
M What is the best way to find all records of an e-mail for our company? Outlook VBA and Custom Forms 2
J Best approach for restarting custom task form development after seemingly corrupt form environment Outlook VBA and Custom Forms 0
X Best utility for repairing PST files? Using Outlook 3
S Best method to use Outlook Standalone with iPhone Using Outlook 13
D a suggestion ...sent with the best intentions Using Outlook 1
B What is the best way to use Outlook address book to select customer and then open Excel Outlook VBA and Custom Forms 22
M How best to track OUTGOING referrals? BCM (Business Contact Manager) 2
M best duplicate contact remover? Using Outlook 6
jobudge Reinstalled Outlook 2013 after system crash what is best way to synch with gmail? Using Outlook 1
M Best way to identify/designate contacts in incoming emails? Using Outlook 4
P Best way to locally backup/archive Gmail? Using Outlook 1
M Looking for options and best practices for an Edge Server (Exchange or not) Exchange Server Administration 0
O What are best practices for a re-install Office 13 w/BCM with OS re-install BCM (Business Contact Manager) 1
G what is the best way to organize outlook contacts Using Outlook 1
D What would be the best strategy? Using Outlook 3
crazyboy BCM Installation Best Practices? BCM (Business Contact Manager) 5
J Best way to move form old to new, larger pst? Using Outlook 2
M Best way to move outlook? Using Outlook 1
R Best way to share outlook calendar with 10 laptops Using Outlook 9
B Best way to get a pick-list on a form? Outlook VBA and Custom Forms 1
M Best way to distribute VBA project Outlook VBA and Custom Forms 4
E Where is the best place to store values for combobox entries Outlook VBA and Custom Forms 5
E Where is the best place to store values for combobox entries Outlook VBA and Custom Forms 5
T Is Explorer.FolderSwitch the best event for hiding commandbarbutton? Outlook VBA and Custom Forms 5
M Outlook 2007 / Vista / Windows XP best combo needed BCM (Business Contact Manager) 2
C Form Region, Task Pain, Ribbon Extension - which way is best? Outlook VBA and Custom Forms 1
U Catching ModuleSwitch events after "open in new window" Outlook VBA and Custom Forms 2
oliv- property "is printed" or catching print events Outlook VBA and Custom Forms 2

Similar threads

Back
Top