none
How to catch the changes in Outlook To email addresses list? RRS feed

  • Question

  • I am working on Outlook add-in task where I have to catch a change in the To... address textbox and show some relevant information in the Inspector window. I wonder, if Outlook API raises any events when To... address line is changed? Thanks

    Sunday, October 1, 2017 11:09 PM

Answers

  • Of course - MailItem.PropertyChange event will fire for the To, CC, and BCC properties.

    You can see these events live in OutlookSpy - in the inspector window, click CurrentItem button on the OutlookSpy ribbon, go to the Event tab, look at the log at the bottom of the page.


    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 SZBM Thursday, October 5, 2017 3:47 AM
    Monday, October 2, 2017 12:34 AM
  • Hi SZV_KBM,

    What do you want to do with To address event? I made a test with PropertyChange with below code, it will fire multiple times while changing To field.

    Dim WithEvents myInspector As Outlook.Inspectors
    Dim WithEvents myMailItem As Outlook.MailItem
    
    Private Sub Application_Startup()
        Set myInspector = Application.Inspectors
    End Sub
    
    Private Sub myInspector_NewInspector(ByVal Inspector As Outlook.Inspector)
        If TypeOf Inspector.CurrentItem Is MailItem Then
            Set myMailItem = Inspector.CurrentItem
        End If
    End Sub
    
    Private Sub myMailItem_PropertyChange(ByVal Name As String)
    On Error GoTo ErrorCatcher
        If Name = "To" Then
            Debug.Print myMailItem.To
        End If
        Exit Sub
    ErrorCatcher:
        MsgBox Err.Description
    End Sub
    

    You could refer below link for more information.

    # MailItem.PropertyChange event is fired several times for To field changed

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/8d289eea-0b57-4a6f-b38e-d49512f71cfb/mailitempropertychange-event-is-fired-several-times-for-to-field-changed?forum=vsto

    Best Regards,

    Edward


    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 SZBM Thursday, October 5, 2017 3:47 AM
    Monday, October 2, 2017 7:14 AM

All replies

  • Of course - MailItem.PropertyChange event will fire for the To, CC, and BCC properties.

    You can see these events live in OutlookSpy - in the inspector window, click CurrentItem button on the OutlookSpy ribbon, go to the Event tab, look at the log at the bottom of the page.


    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 SZBM Thursday, October 5, 2017 3:47 AM
    Monday, October 2, 2017 12:34 AM
  • Hi SZV_KBM,

    What do you want to do with To address event? I made a test with PropertyChange with below code, it will fire multiple times while changing To field.

    Dim WithEvents myInspector As Outlook.Inspectors
    Dim WithEvents myMailItem As Outlook.MailItem
    
    Private Sub Application_Startup()
        Set myInspector = Application.Inspectors
    End Sub
    
    Private Sub myInspector_NewInspector(ByVal Inspector As Outlook.Inspector)
        If TypeOf Inspector.CurrentItem Is MailItem Then
            Set myMailItem = Inspector.CurrentItem
        End If
    End Sub
    
    Private Sub myMailItem_PropertyChange(ByVal Name As String)
    On Error GoTo ErrorCatcher
        If Name = "To" Then
            Debug.Print myMailItem.To
        End If
        Exit Sub
    ErrorCatcher:
        MsgBox Err.Description
    End Sub
    

    You could refer below link for more information.

    # MailItem.PropertyChange event is fired several times for To field changed

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/8d289eea-0b57-4a6f-b38e-d49512f71cfb/mailitempropertychange-event-is-fired-several-times-for-to-field-changed?forum=vsto

    Best Regards,

    Edward


    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 SZBM Thursday, October 5, 2017 3:47 AM
    Monday, October 2, 2017 7:14 AM
  • Hi Edward,

    Thank you for your advice. I want to handle address changes to get some DB data for a relevant recipient and show it in inspector task pane. As it was discussed in the thread link that you referred, TO PropertyChange event fires multiple times if email has more than one recipient. In my case, multiple DB queries create a noticeable response lag. I wonder, what is the best way of dealing with multiple event firing? I don't think the timer idea discussed in the link is appropriate for my case. This would create redundant DB queries on each timer time elapsed event.

    Thanks,

    Kind regards,

    SZV_KBM


    • Edited by SZBM Monday, October 9, 2017 8:30 PM
    Monday, October 9, 2017 8:27 PM
  • Timer will work just fine - it must be disabled (Timer.Enabled = false) at the very first line of your timer event handler. It will never fire again unless your PropertyChange event handler re-enables it.

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

    Monday, October 9, 2017 8:35 PM
  • Hi SZV_KBM,

    Did the suggestion from Dmitry work for you? If not, for this new specific issue, I would suggest you post a new thread, and share us more information, then we could focus on discussing this specific issue.

    Thanks for your understanding.

    Best Regards,

    Edward


    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.

    Tuesday, October 10, 2017 2:28 AM
  • Thank you, Dmitry. Let's assume the timer_Elapsed method retrieves DB data and refreshes the inspector task pane. Now, if an email has two recipients and user adds a third one, the PropertyChange event will fire three times. The timer will start and stop three times accordingly. Although, the subsequent redundant timer_Elapsed DB calls are eliminated, the two redundant calls are still performed when a third recipient is added. Am I missing something? Thanks




    • Edited by SZBM Tuesday, October 10, 2017 3:46 AM
    Tuesday, October 10, 2017 2:57 AM
  • The timer callback will only fire once unless the PropertyChange event fires witg a frequency lower than the timer interval.

    Each PropertyChange event handler must reset the timer (Enabled =- false / Enabled = true), so the timer event handler will fire after the timer interval elapses and there were no new PropertyChange callbacks.

    Try it, it is really simple to test. Just make sure you are using the Timer object from the Forms namespace.


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

    Tuesday, October 10, 2017 4:23 AM
  • Thank you, Dmitry. I've tested your suggestion and it works. This is the code that I tested

    using System;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using System.Windows.Forms;
     
    namespace PropertyChangeEventHandler
    {
        public partial class ThisAddIn
        {
            Outlook.Inspectors inspectors;
            Outlook.Inspector _inspector;
            Outlook.MailItem item;
            Timer propertyChangeTimer ;
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                inspectors = this.Application.Inspectors;
                inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            }
     
     
            void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector inspector)
            {
                propertyChangeTimer = new Timer();
                propertyChangeTimer.Interval = 5000;
                propertyChangeTimer.Tick += propertyChangeTimer_Tick;
     
                item = inspector.CurrentItem as Outlook.MailItem;
                if (item == null)
                {
                    return;
                }
     
                ((Outlook.ItemEvents_Event)item).PropertyChange += Inspector_PropertyChange;
     
                 _inspector = inspector;
            }
     
     
            void propertyChangeTimer_Tick(object sender, EventArgs e)
            {
                propertyChangeTimer.Stop();
                Outlook.MailItem mailItem = _inspector.CurrentItem as Outlook.MailItem;
     
                if (mailItem != null)
                {
                    if (mailItem.EntryID == null)
                    {
                        mailItem.Subject = "The system time is " + System.DateTime.Now.ToString();
                    }
                }
            }
     
            private void Inspector_PropertyChange(string name)
            {
                if (name != "To")
                {
                    return;
                }
     
                propertyChangeTimer.Start();
     
            }
    

    Tuesday, October 10, 2017 9:40 PM
  • A few things.

    1. Inspector_PropertyChange must call Stop, then Start, otherwise the timer is not reset.

    2. Your code only handles a single open inspector. To avoid that limitation, create a wrapper class that stores the pointers to the Inspector and Mailtem as its members. propertyChangeTimer_Tick and Inspector_PropertyChange must be its members. That wrapper class must be created when your Inspectors_NewInspector event fires.


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

    Tuesday, October 10, 2017 10:29 PM
  • Agree. I tried to keep the test prototype code as simple as possible. 
    • Edited by SZBM Tuesday, October 10, 2017 11:08 PM
    Tuesday, October 10, 2017 11:07 PM