none
"The operation failed." and "The Explorer has been closed and cannot be used for further operations. Review your code and restart Outlook." errors

    Question

  • Hi There,

    I am facing some issues with below code. Could you please help me to resolve this?

    I have below code:

            public object GetOlkExpItem(Explorer _explorer)
            {
                try
                {
                    if (_explorer == null || _explorer.Selection == null || _explorer.Selection.Count == 0) return null; //This line throws error
                    return _explorer.Selection[1];
                }
                catch (Exception exception) 
                {
                    MessageBox.Show("ERROR: GetOlkExpItem failed. " + exception.Message);
                }
                return null;
            }

    Errors encountered as below:

    Error 1.

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0xF5920009): The Explorer has been closed and cannot be used for further operations. Review your code and restart Outlook.
       at Microsoft.Office.Interop.Outlook._Explorer.get_Selection()


    Error 2.

    Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x88304005): The operation failed. at Microsoft.Office.Interop.Outlook._Explorer.get_Selection()

    Please advise.

    Thanks in advance.


    sureshh...

    Tuesday, November 20, 2012 6:38 PM

All replies

  • Where and when did you retrieve the Explorer object? Are you keeping a poinmter to a stale expolorer that the user has already closed?

    Why not use Application.ActiveExplorer?


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

    Tuesday, November 20, 2012 7:32 PM
  • when do you invoke that function?
    Tuesday, November 20, 2012 7:42 PM
  • Hi,

    Sorry I was off this thread for sometime. To answer your questions

    Where and when did you retrieve the Explorer object? -> I retrieved main Explorer object on Outlook startup and keep its reference.
    Are you keeping a poinmter to a stale expolorer that the user has already closed? -> I am not keeping any such pointer.
    Why not use Application.ActiveExplorer? -> Do you think using this way will resolve this issue?

    when do you invoke that function?-> I invoke this function whenever SelectionChange event gets fired on main outlook explorer.


    Sergey mentioned in his reply in one of the similar thread at "http://www.add-in-express.com/forum/read.php?FID=5&TID=7680", that "Also please check if you wrapped the call to the Selection property in try...catch. The fact is that if the selection is empty, the Selection property may produce the exception."

    As you know, I already have my code in try...catch block and I am catching this exception. I am not able to understand why this happens in my case even though I have possible checks like below in my code:

     if (_explorer == null || _explorer.Selection == null || _explorer.Selection.Count == 0)

    Please advise.

    Thanks,

    Suresh


    sureshh...

    Monday, December 31, 2012 4:26 PM
  • Where and when did you retrieve the Explorer object? -> I retrieved main Explorer object on Outlook startup and keep its reference.
    Are you keeping a poinmter to a stale expolorer that the user has already closed? -> I am not keeping any such pointer.

    I am confused by your last statement - if you retrieve the Explorer object on startup (what do you do if there are no explorers?), that means you are storing a pointer to that object in a variable and use it later. How can your last statement be true iff the user closes the explorer and the object obviously becomes invalid?

    Why not use Application.ActiveExplorer? -> Do you think using this way will resolve this issue?Why not use Application.ActiveExplorer? -> Do you think using this way will resolve this issue?

    I don't know, but since you are the one experiencing thsi problem, why don't you try that?



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

    Monday, December 31, 2012 4:33 PM
  • Hi Dmitry,

    AFAIK the only case where we wont be having any explorers is when we close the Outlook completely, isn't it? So we are using that object only when Outlook is open and not when Outlook closes.

    Ok I will try Application.ActiveExplorer object instead of that. I will keep you posted if this resolve my issue.

    But I am still confused why it really throws that error even if I have these checks in place.

    Thanks,

    Suresh


    sureshh...

    Monday, December 31, 2012 4:43 PM
  • No, you will have no explorers if you start Outlook without opening any folders.

    That can happen if an instance of Outlook is created programmatically or if Outlook starts to display a new inspector only, e.g. when it pops up when you click on a mailto link.

    Your checks will do you no good if you keep a reference to an Outlook.Explorer object and it gets closed. Your variable != null, but internally Outlook will raise an error since even though the COM object is still alive, there is no Explorer window anymore.


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

    Monday, December 31, 2012 5:15 PM
  • I agree, that's one  case where Outlook opens with only inspector and no explorers.

    Dmitry, Could you please advise me on how can I check if that explorer is in open or in closed state in such cases to make sure? 

    As at some other place in my code I am also receiving error like this:
    "The explorer  has been closed and cannot be used for further operations. Review your code and restart Outlook."


    sureshh...

    Monday, December 31, 2012 5:31 PM
  • The only way to check is to actually attempt to use the Explorer object and catch the error.

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

    Monday, December 31, 2012 5:42 PM
  • Thats really awful.

    sureshh...

    Monday, December 31, 2012 6:25 PM
  • Well, if you really need to track a *particular* explorer, you must trap the Explorer.Close event and release your reference. 

    In your case (accessing the selected items) however, there is absolutely no reason to hold a reference to an Explorer object - just use Aplication.AcvtiveExplorer whenever you need to access the selection.


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

    Monday, December 31, 2012 6:51 PM