开发者

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

开发者 https://www.devze.com 2023-03-30 05:12 出处:网络
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

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 Micro开发者_C百科soft.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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消