none
How to associate the properties obtained from NewInspector event with correct original mail? RRS feed

  • Question

  • Hi,

    I use VS2010 + outlook 2007 to develop an Add-In:  I need to find the original mail if exists in the database based on its properties when Reply or ReplyAll fired. If exists, it status will be updated.

    Now I have got the properties of the original mail through the Inspectors.NewInspector event. But the mail opened with inspector does not always need to reply, and here may be multiple inspectors open at a time. so, how to associate the properties with correct original mail? Thanks in advance!

    Private m_inspectors As Outlook.Inspectors
     
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    	m_inspectors = Globals.ThisAddIn.Application.Inspectors
    	AddHandler m_inspectors.NewInspector, AddressOf GetProperties
    	
    	' Handle also already existing Inspectors (e.g. Double clicking a .msg file) 	
    	For Each inspector As Outlook.Inspector In m_inspectors
    		GetProperties(inspector)
    	Next
    
    End Sub
    
    
    Private Sub GetProperties(ByVal inspector As Outlook.Inspector)
    	Dim obj As Object = inspector.CurrentItem
    	If (TypeOf obj Is Outlook.MailItem) Then
    		MsgBox("obj:" & vbCrLf & obj.Subject & vbCrLf & obj.ReceivedTime & vbCrLf & obj.SenderEmailAddress)
    	End If
    End Sub

    Saturday, February 25, 2017 2:45 AM

Answers

  • Hello,

    Please download the sample code from Outlook 2010: Developing an Inspector Wrapper.

    And then add event handler in MailItemWrapper.vb

        Protected Overloads Overrides Sub Initialize()
            ' Get the Item of the current Inspector
            Item = DirectCast(Inspector.CurrentItem, Outlook.MailItem)
    
            ' Register for the Item events
            AddHandler Item.Open, AddressOf Item_Open
            AddHandler Item.Write, AddressOf Item_Write
            AddHandler Item.Reply, AddressOf Item_Reply
        End Sub
    
        Private Sub Item_Reply(Response As Object, ByRef Cancel As Boolean)
            Item = DirectCast(Inspector.CurrentItem, Outlook.MailItem)
            MsgBox("Subject:" & Item.Subject & vbCrLf & "ReceivedTime:" & Item.ReceivedTime & vbCrLf & "ID:" & Id.ToString)
        End Sub

    When you replying the mail, it shows different properties, I think it is because it warps the new created inspector of the new mail item.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by leon1526 Friday, March 3, 2017 11:16 AM
    Friday, March 3, 2017 8:58 AM
    Moderator
  • You have already asked this question, and the answer is still the same. https://social.msdn.microsoft.com/Forums/en-US/6ac8a266-37ed-4c58-a37b-aeff4fb1f496/how-to-get-the-properties-of-the-msg-file?forum=outlookdev

    You need to be able to track all user actions that can lead the user to reply to an email.

    From the old answer:

    What you need to do is track the Explorer.SelectionChange event and set up event handlers on the selected items. You also need to subscribe to the Application.Inspectors.NewInspector event and set up event sink on the Inspector.CurentItem object.

    You can then use MailItem.Reply/ReplyAll/Forward events to access the newly created response as well as the original message.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Saturday, February 25, 2017 2:57 PM
  • 1, Because a user can reply either from the Explorer by selecting a message, and the clicking Reply. Or the user can open the message first (NewInspector event will fire, then click Reply).

    2. The original message is the message raising the event. If can be pretty complicated in VBA, but in C# or VB.Net, you would wrap the object that raises the event in a class and expose the event handler as its method. The original object would be stored as a property. When the event handler fires, you can access the property of that class. See https://msdn.microsoft.com/en-us/library/ff973716.aspx for an example.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Sunday, February 26, 2017 3:42 AM
  • MailItem.Reply event has two parameters - Response (the new item being created) and Cancel parameter (by ref).

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Sunday, February 26, 2017 4:58 PM

All replies

  • You have already asked this question, and the answer is still the same. https://social.msdn.microsoft.com/Forums/en-US/6ac8a266-37ed-4c58-a37b-aeff4fb1f496/how-to-get-the-properties-of-the-msg-file?forum=outlookdev

    You need to be able to track all user actions that can lead the user to reply to an email.

    From the old answer:

    What you need to do is track the Explorer.SelectionChange event and set up event handlers on the selected items. You also need to subscribe to the Application.Inspectors.NewInspector event and set up event sink on the Inspector.CurentItem object.

    You can then use MailItem.Reply/ReplyAll/Forward events to access the newly created response as well as the original message.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Saturday, February 25, 2017 2:57 PM
  • Hi Dmitry, thank you very much for your reply.

    I now know how to use Explorer.SelectionChange, Application.Inspectors.NewInspector event and MailItem.Reply / ReplyAll events separately, but I still do not know how to put them together to work.

    I am a beginner and I hope someone can explain a little more detail, or give me a step-by-step- instruction. Many thanks in advance!

    Saturday, February 25, 2017 4:08 PM
  • So which part exactly are you having troubles with?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, February 25, 2017 5:47 PM
  • Hi Dmitry,

    My questions are as follows.

    First, I used the Inspectors.NewInspector event to get the properties of the message, so I do not know why I still need to use the Explorer.SelectionChange event.

    Secondly, I do not understand how to access the original message in the MailItem.Reply / ReplyAll event.

    Thanks in advance!

    Sunday, February 26, 2017 3:24 AM
  • 1, Because a user can reply either from the Explorer by selecting a message, and the clicking Reply. Or the user can open the message first (NewInspector event will fire, then click Reply).

    2. The original message is the message raising the event. If can be pretty complicated in VBA, but in C# or VB.Net, you would wrap the object that raises the event in a class and expose the event handler as its method. The original object would be stored as a property. When the event handler fires, you can access the property of that class. See https://msdn.microsoft.com/en-us/library/ff973716.aspx for an example.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Sunday, February 26, 2017 3:42 AM
  • Hi Dmitry,

    I tested that opening a message in the explorer also triggers the NewInspector event, so the SelectionChange event does not seem necessary,right?

    That example code I have seen before, but some of the things involved are too hard for me. Maybe I should first learn more the knowledge of OOP. Anyway, thanks for your help!

    Sunday, February 26, 2017 4:04 AM
  • You can reply to the selected message(s) without opening them. NewInspector event will fire for the newly created reply, but you still will not know what the original message was since you can have multipel explorers open.

    You also need Explorer.InlineResponse event since messages can be replied to inline without opening an inspector.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, February 26, 2017 4:56 PM
  • MailItem.Reply event has two parameters - Response (the new item being created) and Cancel parameter (by ref).

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by leon1526 Friday, March 3, 2017 11:15 AM
    Sunday, February 26, 2017 4:58 PM
  • 1, Because a user can reply either from the Explorer by selecting a message, and the clicking Reply. Or the user can open the message first (NewInspector event will fire, then click Reply).

    2. The original message is the message raising the event. If can be pretty complicated in VBA, but in C# or VB.Net, you would wrap the object that raises the event in a class and expose the event handler as its method. The original object would be stored as a property. When the event handler fires, you can access the property of that class. See https://msdn.microsoft.com/en-us/library/ff973716.aspx for an example.




    Hi Dmitry,

    I took the time to learn OOP. According to https://msdn.microsoft.com/en-us/library/ff973716.aspx I succeeded in getting the properties of the original message.

    Initially I thought that through this ID I can get the properties of original message when replying.

    But then I found that when I opened and reply to the message will trigger separately a NewInspector event as shown below. The two Inspector IDs are inconsistent.

     

    When open the mail

    ---------------------------

    Subject:Upgrade request...

    ReceivedTime:2017/3/1 15:02:23

    ID:20ab0b7c-1edf-46bb-8fdb-8b12922568b1

     

    When Reply or Reply to all

    ---------------------------

    Subject:re:Upgrade request...

    ReceivedTime:4501/1/1

    ID:bf69ea9c-f8c8-4d9c-881b-ff0f7656d8bb

     

    That is, I still cannot find the properties of the original message when replying. How should I do? What is the relationship between the two Inspectors? Or more directly that the two messages have any relevance?


    Wednesday, March 1, 2017 12:57 PM
  • What inspector id do you mean? There is no such thing. Please show the relevant snippets of your code.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, March 1, 2017 4:06 PM
  • What inspector id do you mean? There is no such thing. Please show the relevant snippets of your code.



    Hi Dmitry,

    This ID is a property in the base class(InspectorWrapper). The base class and the derived class(MailItemWrapper) are exactly the same as those in the example.

    The following is the modified code in thisAddin.

    Public Class ThisAddIn
    
        Private m_inspectors As Outlook.Inspectors
        Private m_wrappedInspectors As Dictionary(Of Guid, InspectorWrapper)
    
        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            m_wrappedInspectors = New Dictionary(Of Guid, InspectorWrapper)
            m_inspectors = Globals.ThisAddIn.Application.Inspectors
            AddHandler m_inspectors.NewInspector, AddressOf WrapInspector
    		
            For Each inspector As Outlook.Inspector In m_inspectors
                WrapInspector(inspector)
            Next
    
        End Sub
    
        Private Sub WrapInspector(ByVal inspector As Outlook.Inspector)
    
            Try
                Dim wrapper As InspectorWrapper = InspectorWrapper.GetWrapperFor(inspector)
                If wrapper IsNot Nothing Then
                    AddHandler wrapper.Closed, AddressOf wrapper_Closed
                    m_wrappedInspectors(wrapper.id) = wrapper
                End If
    
                Dim mail As New MailItemWrapper(inspector)
                MsgBox("Subject:" & mail.Item.Subject & vbCrLf & "ReceivedTime:" & mail.Item.ReceivedTime & vbCrLf & "ID:" & mail.id.ToString)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")
            End Try
    
        End Sub
    
    End Class
    

    Thursday, March 2, 2017 12:31 AM
  • Hello,

    Please download the sample code from Outlook 2010: Developing an Inspector Wrapper.

    And then add event handler in MailItemWrapper.vb

        Protected Overloads Overrides Sub Initialize()
            ' Get the Item of the current Inspector
            Item = DirectCast(Inspector.CurrentItem, Outlook.MailItem)
    
            ' Register for the Item events
            AddHandler Item.Open, AddressOf Item_Open
            AddHandler Item.Write, AddressOf Item_Write
            AddHandler Item.Reply, AddressOf Item_Reply
        End Sub
    
        Private Sub Item_Reply(Response As Object, ByRef Cancel As Boolean)
            Item = DirectCast(Inspector.CurrentItem, Outlook.MailItem)
            MsgBox("Subject:" & Item.Subject & vbCrLf & "ReceivedTime:" & Item.ReceivedTime & vbCrLf & "ID:" & Id.ToString)
        End Sub

    When you replying the mail, it shows different properties, I think it is because it warps the new created inspector of the new mail item.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by leon1526 Friday, March 3, 2017 11:16 AM
    Friday, March 3, 2017 8:58 AM
    Moderator