none
Inspector.CurrentItem.Close() is not working RRS feed

  • Question

  • I am trying to open a mail ("C:\Mail.msg" file) from a fileserver location and when i close it, it leaves some references. I am unregistereing all the used events onIsnpectorClose method. but still it leaves some references and getting the below error when i am trying to access the file ("C:\Mail.msg")

    My sample code...

    onIsnpectorClose ()

    public Inspector Inspector { get; set; } 
    public MailItem MailItem { get; set; } 
    if (Inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem &&
    Inspector.CurrentItem == MailItem) 
    {
    Inspector.CurrentItem.Close(OlInspectorClose.olDiscard); 
    }
    }

    Error Message

    Cannot open file "C:\Mail.msg" The file may not exist, you may not have permission to open it, or it may be open in another program.


    How can i close the mail item without leaving any reference behind? Kindly advise.


    http://www.selvaonline.com - Live a Happy & Balanced Life


    • Edited by SelvaOnline Friday, March 21, 2014 7:26 PM Updated the question.
    Friday, March 21, 2014 6:51 PM

Answers

  • After i have moved the releasecomobject method to finally block and in finally block i have changed the RleaseComObject to Marshal.Finalreleaseobject then it started working fine.

    http://www.selvaonline.com - Live a Happy & Balanced Life

    • Marked as answer by SelvaOnline Monday, March 24, 2014 7:54 PM
    Monday, March 24, 2014 7:54 PM

All replies

  • The error (A required parameter was omitted.you need to pass one of the olInspectorClose enum values. olDiscard would be a good choice in your case.


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

    Friday, March 21, 2014 6:57 PM
  • Thanks for your reply. I have updated my question with your help.

    But the below code is not properly closing the mailitem("C:\Mail.msg") and it still leave the reference of the file and i am getting the below error message when i am accessing the file.

    Inspector.CurrentItem.Close(OlInspectorClose.olDiscard);

    Error Message

    Cannot open file "C:\Mail.msg" The file may not exist, you may not have permission to open it, or it may be open in another program.


    http://www.selvaonline.com - Live a Happy & Balanced Life


    Friday, March 21, 2014 7:20 PM
  • You need to completely release the message and the inspector using Marshal.ReleaseComObject().

    What is your code that opens and display the message?


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

    Friday, March 21, 2014 7:29 PM
  • After i completely release inspector and message also i am getting the same issue :(

    I am not hooking into message display events. I am just setting up the Inspector when open a message.Message uses the defaul outlook inbuilt methods to display it as per my understanding.


    http://www.selvaonline.com - Live a Happy & Balanced Life


    Friday, March 21, 2014 8:23 PM
  • I'd recommend choosing another drive or location for storing a mail item. The system drive is protected.
    Friday, March 21, 2014 8:32 PM
  • It is not the issue with the system drive. Whereever is store the same issue exist. Prooblem with the plugin which is not closing the message properly and leaving some reference behind. I am not seeing this issue if i disable the plugin.

    http://www.selvaonline.com - Live a Happy & Balanced Life


    Friday, March 21, 2014 8:45 PM
  • You are using multiple dot notation, so you are guaranteed to run into implicit variables that you cannot release.

    How do you know that the message displayed comes from an MSG file? Because you are the one who displays it? How do you do that?


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

    Friday, March 21, 2014 8:49 PM
  • Well, you need to review all your code and release all underlying COM objects instantly. For example:

     if (Inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem && 
                                                     Inspector.CurrentItem == MailItem) 
    

    The CurrentItem property of the Inspector class returns an instance of Outlook item (it can be a MailItem object for example). So, it should be released each time you call the property. I'd suggest get the Outlook item once then use it in the if statement:

    object currentItemObj = inspector.CurrenrItem;
    if (currentItemObj is Microsoft.Office.Interop.Outlook.MailItem && 
                                                     currentItemObject == MailItem) 
    // .. some code goes here
    Marshal.ReleaseComObject(currentItemObject);
    
    

    Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. See Systematically Releasing Objects for more information.

    Friday, March 21, 2014 8:54 PM
  • After i have moved the releasecomobject method to finally block and in finally block i have changed the RleaseComObject to Marshal.Finalreleaseobject then it started working fine.

    http://www.selvaonline.com - Live a Happy & Balanced Life

    • Marked as answer by SelvaOnline Monday, March 24, 2014 7:54 PM
    Monday, March 24, 2014 7:54 PM