none
Which Event Fires When Email Item Closes? RRS feed

  • Question

  • Hi

    I have developed an Outlook addin which among other things, detects when a user hits the Send button on an email as follows:

    Public WithEvents oOLApplication As Outlook.Application
    Sub oOLApplication_ItemSend(ByVal Item As Object, Cancel As Boolean)

         <code to prompt the user before the email is sent>

    End Sub

    This works ok

    Now I want to detect when the user closes the current email item. I have tried the following but the Close event doesn't fire.

    Public WithEvents oOLCurrentItem As MailItem
    Private Sub AddinInstance_Initalize_Handler()

        Set oOLCurrentItem = oOLApplication.ActiveInspector.CurrentItem

    End Sub

    Private Sub oOLCurrentItem_Close(Cancel As Boolean)

        <code to prompt the user before the email is closed>
       
    End Sub

    Although the code is in VB6 my question really relates to Outlook events so I think this is the appropriate forum.

    Thanks in return

    Mike

    Monday, August 24, 2015 12:02 PM

Answers

  • Thanks Eugene

    I also found the following code works

    Public WithEvents oOlInspectors As Outlook.Inspectors
    Public WithEvents oOLActiveEmail As MailItem

     Private Sub oOlInspectors_NewInspector(ByVal Inspector As Inspector)
     
        If Inspector.CurrentItem.Class = olMail Then _
                                            Set oOLActiveEmail = Inspector.CurrentItem
     
     End Sub

    Private Sub oOLActiveEmail_Open(Cancel As Boolean)
        
        <code that runs when an email item is opened>

    End Sub

    Private Sub oOLActiveEmail_Close(Cancel As Boolean)
       
        <code that runs when an email item is closed>

    End Sub

    Private Sub oOLActiveEmail_Reply(ByVal Response As Object, Cancel As Boolean)
       
        <code that runs when an email item is replied>
     
     End Sub


    Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
       
    On Error GoTo error_handler

        Set oOlInspectors = Application.Inspectors

    Tuesday, August 25, 2015 12:45 AM

All replies

  • Hello Mike,

    Yes, this is the right forum for such questions. What Outlook version do you have installed on the PC?

    How did you close the mail item in Outlook? Did you close an inspector window?

    Try to handle the Close event of the Inspector class instead. It is fired when the inspector associated with a Microsoft Outlook item is being closed.

    Note, the ItemSend event is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. It is not fired when the user hits the Send button, you may try to use the Send event instead which is fired when the user selects the Send action for an item (which is an instance of the parent object).

    Monday, August 24, 2015 12:11 PM
  • Thanks for the quick reply Eugene

    I close the email item by either File > Close or clicking the X button.

    From your reply I think I've added the close event code in the wrong place (ie during initializing the addin).

    At this point there is no .ActiveInspector.CurrentItem active.

    Where and when would I access the inspector class in the addin?

    Mike

    Monday, August 24, 2015 12:40 PM
  • Sorry, I have Outlook 2010 running
    Monday, August 24, 2015 12:40 PM
  • Hello Mike,

    You need to handle the NewInspector/Activate events for subscribing to the active inspector events. You can read more about that in the Developing an Inspector Wrapper for Outlook 2010 article.

    How to: Implement a Wrapper for Inspectors and Track Item-Level Events in Each Inspector

    Outlook 2010: Developing an Inspector Wrapper - VB.NET and C# sample projects

    Monday, August 24, 2015 12:53 PM
  • Please see my previous reply.
    Monday, August 24, 2015 12:54 PM
  • Thanks Eugene

    I also found the following code works

    Public WithEvents oOlInspectors As Outlook.Inspectors
    Public WithEvents oOLActiveEmail As MailItem

     Private Sub oOlInspectors_NewInspector(ByVal Inspector As Inspector)
     
        If Inspector.CurrentItem.Class = olMail Then _
                                            Set oOLActiveEmail = Inspector.CurrentItem
     
     End Sub

    Private Sub oOLActiveEmail_Open(Cancel As Boolean)
        
        <code that runs when an email item is opened>

    End Sub

    Private Sub oOLActiveEmail_Close(Cancel As Boolean)
       
        <code that runs when an email item is closed>

    End Sub

    Private Sub oOLActiveEmail_Reply(ByVal Response As Object, Cancel As Boolean)
       
        <code that runs when an email item is replied>
     
     End Sub


    Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
       
    On Error GoTo error_handler

        Set oOlInspectors = Application.Inspectors

    Tuesday, August 25, 2015 12:45 AM