none
mailItem.PropertyChange stops firing RRS feed

  • Question

  • I am implementing Custom Task Panes with E-Mail Messages in Outlook. The core is taken from this link MSDN (Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook)

    Handler for property change is added:

        void TaskPane_VisibleChanged(object sender, EventArgs e)
                {
                    Globals.Ribbons[inspector].ManageTaskPaneRibbon
    .toggleButton1.Checked = taskPane.Visible;
    ...some code here...
    
                    mailItem.PropertyChange += PropertyChangeHandler;
                }

    PropertyChangeHandler checks is recipients have changed and does some heavy routine with POSTs to the server and so on. But... If I add 10 recipients and couple of "cc" and start to remove them with backspace PropertyChangeHandler stops firing at some point. No errors. Buttons on custom task pane work fine.

    What is wrong?

    Seems that either event is eaten or inspector is incorrect, but I cannot spot problem and find the solution.

    I also think that it might be about "heavy load" when next event is fired before previous is completed, but this is a guess

    Thursday, December 4, 2014 1:04 PM

Answers

  • I'd suggest declaring the mailItem object at the class level instead. Most probably the source object is swiped by the GC after the method is finished.
    Thursday, December 4, 2014 2:54 PM

All replies

  • Did you try to save the mail item?

    Outlook caches UI changes and doesn't fire events until switch the cursor to another field or save the item.

    Thursday, December 4, 2014 1:13 PM
  • Does not help.

    If it stops firing, I can make it work only reactivating the add-in, which does the event set-up.

    Thursday, December 4, 2014 1:27 PM
  • Could you please show your code?
    Thursday, December 4, 2014 1:41 PM
  • Basic stuff from http://msdn.microsoft.com/en-us/library/bb296010.aspx

    class InspectorWrapper has something else but this relates to the problem

    void TaskPane_VisibleChanged(object sender, EventArgs e) { Globals.Ribbons[inspector].Ribbon.tbtnEnable.Checked = taskPane.Visible; Settings.Default.IsEnabled = taskPane.Visible; Settings.Default.Save(); MailItem mailItem = inspector.CurrentItem as MailItem; if (taskPane.Visible) { if (mailItem != null) { mailItem.PropertyChange += PropertyChangeHandler; } } else { mailItem.PropertyChange -= PropertyChangeHandler; } } void PropertyChangeHandler(string Name) { if (Name.Equals("To")||Name.Equals("CC") ) { Inspector inspector = (Inspector)Globals.ThisAddIn.Application.ActiveInspector(); InspectorWrapper inspectorWrapper = Globals.ThisAddIn.InspectorWrappers[inspector]; CustomTaskPane taskPane = inspectorWrapper.CustomTaskPane; MailItem mailItem = inspector.CurrentItem as MailItem; if (mailItem != null) { var mypane = (taskPane.Control as Pane); to = Utils.GetSMTPAddressForRecipients(mailItem, 1); cc = Utils.GetSMTPAddressForRecipients(mailItem, 2); var json = CreateJson(to, cc) this.SingleWork(json).ContinueWith((t) => mypane.Show(t.Result)); } } catch (System.Exception ex) { throw; } } private async Task<string> SingleWork(string postData) { try { var baseAddress = new Uri(Settings.Default.Url); using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler) { BaseAddress = baseAddress }) { var result = await client.PostAsync("", new StringContent(postData, Encoding.UTF8, "application/json")); if (!result.IsSuccessStatusCode) { return string.Format("Error occurred, the status code is: {0} ({1})", result.StatusCode, (int)result.StatusCode); } string resultContent = await result.Content.ReadAsStringAsync(); return resultContent; } } catch (System.Exception ex) { Logger.Log("SingleWork failed: Message:" + ex.Message + " InnerException:" + ex.InnerException + " Data:" + postData); return "fail"; } }

    and this is in pane.cs

            public void Show(string responce)
            {
                Stream s = Utils.GenerateStreamFromString(responce);
                webBrowser1.DocumentStream = s;     
            }




    Thursday, December 4, 2014 2:27 PM
  • I'd suggest declaring the mailItem object at the class level instead. Most probably the source object is swiped by the GC after the method is finished.
    Thursday, December 4, 2014 2:54 PM
  • Heavily load limitation is only applicable to the MAPI events. PropertyChange even is pure OOM, MAPI is not involved.

    How long does it take for your event handler to execute?

    Do you see the event fire in OutlookSpy (click CurrentItem button on the OutlookSpy inspector ribbon, go to the Event tab, look at the log at the bottom of the window)?


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

    Thursday, December 4, 2014 2:55 PM