Connecting to an existing Outlook process

2

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"));
c#
outlook
office-interop
asked on Stack Overflow Jun 26, 2010 by Patrick Linskey • edited Jun 26, 2010 by Patrick Linskey

2 Answers

2

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

answered on Stack Overflow Jun 29, 2010 by Patrick Linskey
0

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).

answered on Stack Overflow Jun 26, 2010 by Mitch Wheat

User contributions licensed under CC BY-SA 3.0