VSTO Outlook 2007 Addin Startup throws System.Runtime.InteropServices.COMException (0x80020009): Cannot complete the operation. You are not connected

1

We are building an Outlook 2007 add-in using VSTO. We have code in the initialization logic of the addin which retrieves the MAPIOBJECT associated with the Application.Session. The code in question is as follows:

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        try
        {
            var addIn = Globals.ThisAddIn;
            var application = addIn.Application;
            var mapiObject = application.Session.MAPIOBJECT;
        }
        catch (Exception e)
        {
            // HANDLE ERROR
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
                        // ...
    }

    // Other VSTO generated code

}

This code works like a charm for most times. However, we run into the following error when we attempt to access the MAPIOBJECT property from the Session property:

System.Runtime.InteropServices.COMException (0x86220009): Cannot complete the operation. You are not connected.
  at Microsoft.Office.Interop.Outlook._NameSpace.get_MAPIOBJECT()
  at MyTestAddIn.ThisAddIn.ThisAddIn_Startup(object, System.EventArgs) in C:\foo\MyTestAddIn\ThisAddIn.cs:line 19

We are unable to understand why the code works most of the time but fails at a few. We thought this issue was due a network interruption/user working offline. We tried several combinations of connection states with intentionally working offline from Outlook to disconnecting the network cable when Outlook is about to load the add-in. But we have not been able to reproduce the problem.

Any help is appreciated.

c#
com
outlook
vsto
mapi
asked on Stack Overflow Aug 23, 2011 by oddjobsman

1 Answer

0

It's usually a bad idea to ask for the MAPI session that early, because it might not be properly logged on yet. Why not ask for the session the moment you actually need it?

answered on Stack Overflow Aug 23, 2011 by Paul-Jan

User contributions licensed under CC BY-SA 3.0