none
Not Releasing all objects correctly? RRS feed

  • Question

  • First time trying to work with data from the Outlook 2010 application (or any other version).  Probably a newbie error.

    I'm selecting messages in the Outlook inbox, dragging and dropping them into a text box (or form - same difference), and performing an operation with the body of each message in my own app. 

    It works fine the first time.  But the items never seem to un-select in Outlook.  Clicking on other items in the inbox list will not get them to show up in the preview pane, and opening one item actually opens all that I had originally selected.  Once my app is closed, Outlook seems to behave normally again.  Here's the code:

     private void textBox1_DragDrop(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.None;
                Microsoft.Office.Interop.Outlook.Application OL = new Microsoft.Office.Interop.Outlook.Application();
                
                for (int i = 1; i <= OL.ActiveExplorer().Selection.Count; i++)
                {
                    MessageDroppedEventArgs eRcvd = new MessageDroppedEventArgs();
                    Object temp = OL.ActiveExplorer().Selection[i];
    
                    if (temp is Microsoft.Office.Interop.Outlook.MailItem)
                    {
                        Microsoft.Office.Interop.Outlook.MailItem mailitem = (temp as Microsoft.Office.Interop.Outlook.MailItem);
                        string subject = mailitem.Subject;
                        try
                        {
                            if (subject.Contains("Special Report"))
                            {
                                try
                                {
                                    textBox1.Text = mailitem.Subject.ToString() + "\r\n" + textBox1.Text;
                                    eRcvd.MessageBody = mailitem.Body;
                                    OnReceived(eRcvd);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Error in DragDrop: " + ex.Message.ToString());
                                }
                            }
                        }
                        finally
                        {
                            Marshal.FinalReleaseComObject(mailitem);
                        }
                    }
                    else
                        Marshal.FinalReleaseComObject(temp);
                }
            }

    Any help is greatly appreciated. Thanks!

    Wednesday, October 2, 2013 8:21 PM

Answers

  • That sounds vaguely familiar... If I remember correctly, when handling drag/drop from Outlook, you need to request file descriptor/ file contents clipboard formats...

    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 BobDY Wednesday, October 2, 2013 10:17 PM
    Wednesday, October 2, 2013 9:56 PM

All replies

  • Firstly, you might want to avoid using multiple dot notation - try to cache the value of  OL.ActiveExplorer().Selection before entering the loop.

    Secondly, can you comment out all of your drop handler code and bring it back a line at a time until the problem comes back?


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

    Wednesday, October 2, 2013 8:48 PM
  • OL.ActiveExplorer().Selection (or any compound dot operator) creates an Explorer object and a Selection collection that you can't release. You always should create an explicit object variable for each part, then release it.

    Create the objects outside of the loop and instantiate them within the loop. That way you only create 1 instance of each object. In the case of the Explorer and Selection instantiate them once outside the loop.

    Make sure to release temp. When releasing objects it's a good idea to null them before calling FinalReleaseComObject() or ReleaseComObject().

    When removing items from a collection use a down-counting loop. I'm not sure that applies in this case.

    For Outlook 2010 you can explicitly set the Selection object to the item you want. The UI may not refresh unless you switch ActiveExplorer().CurrentFolder and back, either in code or in the UI.


    Ken Slovak MVP - Outlook

    Wednesday, October 2, 2013 8:59 PM
  • Modified it to what you see below.  Still no joy until I close my app.  I'm assuming I did what you asked?  (Possible I misunderstood)  Thanks for the help,

    Bob

     private void textBox1_DragDrop(object sender, DragEventArgs e)
            {
                //e.Effect = DragDropEffects.None;
                Outlook.Application OL = new Outlook.Application();
                Outlook.Selection objSelection = OL.ActiveExplorer().Selection;
                for (int i = 1; i <= objSelection.Count; i++)
                {
                    //MessageDroppedEventArgs eRcvd = new MessageDroppedEventArgs();
                    Object temp = objSelection[i];
    
                    if (temp is Outlook.MailItem)
                    {
                        Outlook.MailItem mailitem = (temp as Outlook.MailItem);
                        string subject = mailitem.Subject;
                        try
                        {
                            if (subject.Contains("Special Report"))
                            {
                                try
                                {
                                    textBox1.Text = mailitem.Subject.ToString() + "\r\n" + textBox1.Text;
                                    //eRcvd.MessageBody = mailitem.Body;
                                    //OnReceived(eRcvd);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Error in DragDrop: " + ex.Message.ToString());
                                }
                            }
                        }
                        finally
                        {
                            Marshal.FinalReleaseComObject(mailitem);
                        }
                    }
                    else
                        Marshal.FinalReleaseComObject(temp);
                }
            }

    Wednesday, October 2, 2013 9:02 PM
  • So can you try to comment out all of that code and bring it back a line or two at a time?

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

    Wednesday, October 2, 2013 9:07 PM
  • Dmitry, I think you're on the right track.  I'm starting to think it has nothing to do with the objects and everything to do with how I handle the DragDrop event.  I commented everything out and it still did the same thing.  Maybe there's something I'm supposed to do to let Outlook know that I handled the event and I'm done with it?
    Wednesday, October 2, 2013 9:30 PM
  • That sounds vaguely familiar... If I remember correctly, when handling drag/drop from Outlook, you need to request file descriptor/ file contents clipboard formats...

    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 BobDY Wednesday, October 2, 2013 10:17 PM
    Wednesday, October 2, 2013 9:56 PM
  • You got it.  I tried the fix in this thread and it worked.  More hassle than I wanted, but I always learn something through the struggle itself.  Thanks Dmitry and Ken!

    Bob

    Here's the link for next poor B@stard:

    http://social.msdn.microsoft.com/Forums/office/en-US/ad331c33-887b-45a3-b819-f2075222f83f/interopoutlookexplorer-freeze-after-use

    Wednesday, October 2, 2013 10:19 PM