开发者

Connecting to an existing Outlook process

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开发者_如何学Python 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"));


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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜