Inspector Wrapper: Receive Events of Multiple Emails

Use an inspector wrapper if you need to subscribe to the events of multiple open messages.

Last modified: 2007/04/04 | Accessed: 28.942  | #50
◀ Previous sample Next sample ▶


Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.

Code in ThisOutlookSession

In order to receive events of an object, you need to declare a variable with the keyword WithEvents in a class module. It's pretty simple as long as the user opens only one item at a time: Then you also need only one variable. Wait for the Newinspector event; if it triggers, you know a new items is going to be opened; if it's a MailItem, save the reference on it in the variable and then you'll receive the events from that MailItem. It's mre difficult in reality though because users often open more than just one item at a time. Since you don't know beforehand the amount of items to track, you cannot use, say, three variables. What, if the user opens four items?

Solve the issue by using a class module. Benefit of a class module is, you write the code only once and create as many instances, that is objects, of it as will be needed.

The following so called Inspector wrapper demonstrates how it works, it also shows the different situations when an item is being closed.

Paste the following code into the ThisOutlookSession module.

tip  How to add macros to Outlook
Private WithEvents m_Inspectors As Outlook.Inspectors
Private m_MyInspectors As VBA.Collection
Private m_lNextKey As Long

Private Sub Application_Startup()
  Set m_Inspectors = Application.Inspectors
  Set m_MyInspectors = New VBA.Collection
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
  On Error Resume Next
  Dim oInspector As cInspector

  Set oInspector = New cInspector
  If oInspector.Init(Inspector, CStr(m_lNextKey)) Then
    m_MyInspectors.Add oInspector, CStr(m_lNextKey)
    m_lNextKey = m_lNextKey + 1
  End If
End Sub

Friend Property Get MyInspectors() As VBA.Collection
  Set MyInspectors = m_MyInspectors
End Property
Determine the "identity" of your emails. Set with SAM the sender, signature and folder for sent items with the help of rules.

Class Module cInspector

Add a new class module via Insert/Class Module. Press F4, and name the it 'cInspector'. Paste the entire following code into this new class module.

Private WithEvents m_Inspector As Outlook.Inspector
Private WithEvents m_Mail As Outlook.MailItem
Private m_IsClosed As Boolean
Private m_sKey As String

Friend Function Init(oInspector As Outlook.Inspector, _
  sKey As String _
) As Boolean
  Dim obj As Object

  If Not oInspector Is Nothing Then
    Set obj = oInspector.CurrentItem
    If TypeOf obj Is Outlook.MailItem Then
      Set m_Mail = obj
      Set m_Inspector = oInspector
      m_sKey = sKey
      Init = True
    End If
  End If
End Function

Private Sub m_Inspector_Close()
End Sub

Private Sub Class_Terminate()
End Sub

Friend Sub CloseInspector()
  On Error Resume Next
  If m_IsClosed = False Then
    m_IsClosed = True
    ThisOutlookSession.MyInspectors.Remove m_sKey
    Set m_Mail = Nothing
    Set m_Inspector = Nothing
  End If
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

The Send Event

There is three ways to close an email:

  1. Send
  2. Close
  3. Delete

A developer needs to know not only which events could be triggered but also the order of the events. The latter two are easy if the item is unchanged. From within Word (Outlook 2003) the Inspector_Close event is triggered first. From within Outlook in contrast the Item_Close event is triggered first. (Since Outlook 2007 you don't need to bother with the Word events.)

If the item was changed, the user gets prompted whether or not he wants to save the changes; the dialog also allows to cancel the closing. In this case our cInspector object shouldn't be closed, too.

  • When closing, Item_Close is triggered first, then the Save dialog is displayed. Since the user could cancel, we don't want to clean up yet in Item_Close.
  • When deleting, the Save dialog is displayed first. If the user clicks YES, then BeforeDelete is triggered first, then Item_Close. A click on NO triggers no event at all.

Outlook 2000 was a problem because it didn't support the BeforeDelete. Thus it wasn't possible to identify if a user was going to delete a changed item.

Private Sub m_Mail_Send(Cancel As Boolean)
  On Error Resume Next
End Sub

The Close Event

For the following function get also GetOutlookVersion.

Private Sub m_Mail_Close(Cancel As Boolean)
  On Error Resume Next
  If GetOutlookVersion < 10 Then
  ElseIf m_Mail.Saved Then
  End If
End Sub

The BeforeDelete Event

This event wasn't available before Outlook XP. If the item was changed, this event will be triggered after the Save dailog.

Private Sub m_Mail_BeforeDelete(ByVal Item As Object, Cancel As Boolean)
End Sub
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message