How to start or attach to Outlook using Interop C#?

0

I'm trying to write a C# program that will either start a new instance of Outlook if none are running, or attach to the first instance if it happens to be running already.

I've found a previous discussion already that pretty much identifies what I'm trying to achieve. The problem with the Microsoft Solution is that some of my Outlook Plugins/Add-ins don't work properly, for example, iManage Filesite/Worksite. The plugin appears to be in the ribbon when Outlook starts, but some functionality is missing and if I try a search etc. Outlook stops responding and crashes. The plugins work properly if outlook is started from the console or like:

System.Diagnostics.Process.Start(@"OUTLOOK.exe");

But then I can never attach to the running Outlook instance because

application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;

Fails with a COM error (0x800401E3 MK_E_UNAVAILABLE). I can only get around this error if I debug the application and step through it. I've tried allowing time for Outlook to start, switching the active window (changing focus), but none of these options work.

c#
outlook
com
interop
asked on Stack Overflow Feb 7, 2017 by Mr. Anderson • edited May 23, 2017 by Community

1 Answer

0

The error message from GetActiveObject most likely means your code and Outlook are running in different security contexts. Make sure neither apps (or both) are running with elevated privileges (Run As Administrator).


User contributions licensed under CC BY-SA 3.0