locked
ItemClose Event fire more times RRS feed

  • Question

  •  I  bind  ItemEvents_10_CloseEventHandler when ribbon load,,i find  MailItemClose function call more times sometime

      private MOutlook.MailItem mailItem;
            private MOutlook.Inspector inspector;
     
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
                InitMember();
    
            }
    
            private void InitMember()
            {
    
                this.inspector = Globals.ThisAddIn.Application.ActiveInspector();
                if (inspector != null)
                {
     
                    if (inspector.CurrentItem is MOutlook.MailItem)
                    {
                        mailItem = inspector.CurrentItem as MOutlook.MailItem;
                        if (mailItem != null)
                        {
                            ((MOutlook.ItemEvents_10_Event)mailItem).Close += new MOutlook.ItemEvents_10_CloseEventHandler(MailItemClose);
                        }
                    }
                }
            }
     
            private void MailItemClose(ref bool cancel)
            {
                if ((ddMailType.SelectedItem == null || ddMailType.SelectedItem.Label == "") )
                {
                    
                    if (DialogResult.Yes != MessageBox.Show("Mail no marked..", "remind", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        cancel = true;
                    }
                }
     
                Marshal.ReleaseComObject(mailItem);
                Marshal.ReleaseComObject(inspector);
            }


    • Edited by Leo Xiong Saturday, October 24, 2015 1:07 AM
    Saturday, October 24, 2015 1:06 AM

Answers

  • Hello,

    You need to unsubscribe to the Close event and set the source object to null when the item is closed. For example:

            private void MailItemClose(ref bool cancel)
            {
                if ((ddMailType.SelectedItem == null || ddMailType.SelectedItem.Label == "") )
                {
                    
                    if (DialogResult.Yes != MessageBox.Show("Mail no marked..", "remind", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        cancel = true;
                    }
                    else
                    {
    			
     ((MOutlook.ItemEvents_10_Event)mailItem).Close -= new MOutlook.ItemEvents_10_CloseEventHandler(MailItemClose);
    } } Marshal.ReleaseComObject(mailItem); mailItem = null; Marshal.ReleaseComObject(inspector); inspector = null; }

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article in MSDN.


    • Edited by Eugene Astafiev Saturday, October 24, 2015 6:59 AM
    • Marked as answer by Leo Xiong Monday, October 26, 2015 7:13 AM
    Saturday, October 24, 2015 6:58 AM

All replies

  • Hello,

    You need to unsubscribe to the Close event and set the source object to null when the item is closed. For example:

            private void MailItemClose(ref bool cancel)
            {
                if ((ddMailType.SelectedItem == null || ddMailType.SelectedItem.Label == "") )
                {
                    
                    if (DialogResult.Yes != MessageBox.Show("Mail no marked..", "remind", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        cancel = true;
                    }
                    else
                    {
    			
     ((MOutlook.ItemEvents_10_Event)mailItem).Close -= new MOutlook.ItemEvents_10_CloseEventHandler(MailItemClose);
    } } Marshal.ReleaseComObject(mailItem); mailItem = null; Marshal.ReleaseComObject(inspector); inspector = null; }

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article in MSDN.


    • Edited by Eugene Astafiev Saturday, October 24, 2015 6:59 AM
    • Marked as answer by Leo Xiong Monday, October 26, 2015 7:13 AM
    Saturday, October 24, 2015 6:58 AM
  • thanks lots  Eugene Astafiev,

    The problem been solved when unsubscribe to the  Close event at function last 

                if (!cancel)
                {
                    ((MOutlook.ItemEvents_10_Event)mailItem).Close -= new MOutlook.ItemEvents_10_CloseEventHandler(MailItemClose);
                }


    and i found shouldn't  set Item to null same time , Otherwise  throwing  System.NullReferenceException here


    tks again..



    • Edited by Leo Xiong Monday, October 26, 2015 7:04 AM
    Monday, October 26, 2015 6:58 AM