views:

42

answers:

2

Hi,

I'm building an application that opens existing mail messages in Outlook. The user may or may not already be running Outlook. All works well if Outlook is not running, but if it's already running I get a COM error (80080005). The internet seems to indicate that this can happen if the existing Outlook process is running with a higher permission level than the app that tries to bind to it.

Is there some other way for me to ask Outlook to open a message, or do I just need to make sure I match permission levels?

Thanks,

-Patrick

EDIT Adding code to original question, as Stack Overflow does not permit meaningful formatting in comments.

I was originally doing the following:

var outlook = new Outlook.Application();

That line works in all cases except the case where I've launched Outlook prior to launching my application. In that case, I get the aforementioned 80080005 error code.

I've changed this to be a bit more COM-explicit:

Application outlook;
try
{
    outlook = (Application)Marshal.GetActiveObject("Outlook.Application");
}
catch (COMException ex)
{
    if (ex.ErrorCode == -2147221021)
        outlook = new ApplicationClass();
    else
        throw;
}

However, that code still does not quite work -- if Outlook is running, I trap an exception whose ErrorCode is 0x800401E3 (MK_E_UNAVAILABLE). But when I attempt to create the new ApplicationClass object, I still get the same 80080005 error code.

I've also tried putting the following into the catch block instead of the new ApplicationClass() line, but there's no difference in behavior:

outlook = (Application) Activator.CreateInstance(
    Type.GetTypeFromProgID("Outlook.Application"));
A: 

Without seeing your code I'm guessing, but it sounds like you are calling CreateObject(). You need to call GetObject() if Outlook is already running.

First, use GetObject to see whether Outlook is already running (You need to catch the error).

Mitch Wheat
I've been using C# interop lib calls: new Application() etc. Thanks for the GetObject() pointer.
Patrick Linskey
Outlook is somewhat strange in that GetObject("", "Outlook.Application") will start a session if it is not already running and will *not* return an error.
Remou
@Remou: I didn't know that. Good info, thanks.
Mitch Wheat
I've added some code and additional notes to the original posting.
Patrick Linskey
A: 

Turns out that the cause of the problem was the debugger -- I was launching Word from Visual Studio's debugger. When launching Word via normal pathways, the 80080005 code goes away.

-Patrick

Patrick Linskey