Skip to content

Solution to create VBA codes for Outlook Inspector Close Event handler

April 23, 2011

 

Put the code below in thisoutlooksession

Note that due to limitations, only ONE inspector object will have this events activated when multiple inspector are opened in outlook. This is due to inherent limitations.

But this example still shows an example of how to hook up event handlers for other object items in outlook besides the Application object

 

Option Explicit

Public WithEvents colInspectors As Outlook.Inspectors
Public WithEvents objInspector As Outlook.Inspector

Private Sub Application_Startup()

    ‘Call the subroutine to initialize the inspectors events
    Init_colInspectorsEvent
   
End Sub

Private Sub Init_colInspectorsEvent()
    ‘Initialize the inspectors events handler
    Set colInspectors = Outlook.Inspectors

End Sub

Private Sub colInspectors_NewInspector(ByVal NewInspector As Inspector)
   
    ‘Please note the limitations of NewInspector Event, sometimes it doesn’t fire AND only one inspector
    ‘is left with the close event
    
    If NewInspector.CurrentItem.Class = olMail Then MsgBox "New mail inspector is opened"
    If NewInspector.CurrentItem.Class = olTask Then MsgBox "New Task inspector is opened"
    If NewInspector.CurrentItem.Class = olContact Then MsgBox "New Contact inspector is opened"

    ‘This is the code that creates a new inspector with events activated
    Set objInspector = NewInspector
   
End Sub

Private Sub objInspector_Close()
   
    ‘You can do more powerful routines by checking the class of the inspector
    ‘that is closing, was it a mailitem, contactitem or taskitem
    ‘Example
   
    If objInspector.CurrentItem.Class = olMail Then MsgBox "Mail inspector is closing"
    If objInspector.CurrentItem.Class = olTask Then MsgBox "Task inspector is closing"
    If objInspector.CurrentItem.Class = olContact Then MsgBox "Contact inspector is closig"
   
End Sub

2 Comments
  1. Thank permalink

    Thanks, this helped me get started. Using the CurrentItem.Class property in the Close() handler, however, gives an error in case you press the send button in an email, even though the CurrentItem exists (Not Is Nothing). You can work around it by using TypeOf(objInspector.CurrentItem) and check against the returned string instead.

  2. Tuca permalink

    Cara vc é o melhor muito obrigado pela ajuda, quase um mês procurando !!!!! 🙂

Leave a comment