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.
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?
User contributions licensed under CC BY-SA 3.0