none
COM Exception accessing Outlook COM instance from .NET RRS feed

  • Question

  •  

    Hello,

     

    The program is supposed to react on an opened Inspector.

    I built the project against the Outlook 2003 Official developer PIA's.

     

    When i access the Outlook COM Object the first time, i attach an Eventhandler to it. Everything works fine.

    The second time the program is called, attaching the Eventhandler will fail and the following Exception will be thrown.

     

    A COM Object that is seperated from it's underlying RCW cannot be used!

     

    This is not supposed to happen, for it is a new instance of the COM Object.

    I know all instances share the same RCW, yet i am not trying to access the same instance, for it works fine

    with Outlook 2003 SP1 and Outook 2007.

    Further more everything in this small app happens within a single thread, so no crosscalls are used to access the outlook instance.

    I already tried more suitable forums, for this could be more Office then .NET related, yet nobody could tell me why this happens.

     

    Again: First call of "InhibitAutoSave()" works out just fine, all follow-ups crash.

    Heres a snippet from my code.

     

    Code Snippet

    [ComVisible(true)]

    public void InhibitAutoSave()

    {

    _outlook = new Outlook.Application();

    _outlook.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(OnNewInspector);

    }

    //..

    private void OnNewInspector(Outlook.Inspector inspector)

    {

    Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;

    item.Write += new Outlook.ItemEvents_10_WriteEventHandler(OnClose);

    }

    // ..

    private void OnClose(ref bool cancel)

    {

    cancel = true;

    }

    Monday, February 4, 2008 4:21 PM

Answers

  • Hi,

     

    I reproduced this issue. Let's have a see of the following three snippet codes:

    1.Your one:   (throw the error randomly after first called)

    Code Snippet

            [ComVisible(true)]
            public void InhibitAutoSave()
            {
                _outlook = new Outlook.Application();
                _outlook.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(OnNewInspector);
            }

            //..

            private void OnNewInspector(Outlook.Inspector inspector)
            {
                Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;
                item.Write += new Outlook.ItemEvents_10_WriteEventHandler(OnClose);
            }

            // ..

            private void OnClose(ref bool cancel)
            {
                cancel = true;
            }

     

    2. Add GC.Collect()  (Always throws the error)

    Code Snippet

            [ComVisible(true)]
            public void InhibitAutoSave()
            {
                _outlook = new Outlook.Application();
                _outlook.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(OnNewInspector);

    GC.Collect();

            }

            //..

            private void OnNewInspector(Outlook.Inspector inspector)
            {
                Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;
                item.Write += new Outlook.ItemEvents_10_WriteEventHandler(OnClose);
            }

            // ..

            private void OnClose(ref bool cancel)
            {
                cancel = true;
            }

     

     

    3 Declare a class scope variable to hold inspectors property: (No errors any more)

    Code Snippet

    Outlook.Inspectors inspectors = null;

            [ComVisible(true)]
            public void InhibitAutoSave()
            {
                _outlook = new Outlook.Application();

    inspectors = _outlook.Inspectors;
    inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(OnNewInspector);

     

    }

            //..

            private void OnNewInspector(Outlook.Inspector inspector)
            {
                Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;
                item.Write += new Outlook.ItemEvents_10_WriteEventHandler(OnClose);
            }

            // ..

            private void OnClose(ref bool cancel)
            {
                cancel = true;
            }

     

     

    Apparently, the exception is only thrown after Eventhandler is collected by GC. Consequently, to avoid this, you can use the third snippet code as a workaround.

     

     

    Thanks

    Ji

    Wednesday, February 6, 2008 8:16 AM
    Moderator