Marshal.GetActiveObject("Outlook.Application")
throws Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
when Outlook is started and continues while it is running until a non-Outlook window becomes the active foreground window.
I am running Outlook Version 1901 Build 11231.20130 on Windows 10 Version 1803 Build 17134.523 (this is my local machine). I've observed client machines that do not behave this way. This is occurring in a WPF application using .NET 4.5.2 and Microsoft.Office.Interop.Outlook version 15.0.4797.1003 obtained via NuGet.
I have repeatedly run the following code. As detailed above, it will succeed once an instance of Outlook has been minimized or a non-Outlook window is set to the foreground, but it can continuously fail (output below) before this has occurred. I have tried to pause for ~20-30 seconds to give Outlook time to load but still get the same result.
// Detect that the active window is an Outlook window
Outlook.Application app = null;
// Optional: Wait for Outlook to load 20-30 sec using Thread.Sleep
try
{
Debug.WriteLine("app");
app = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
Debug.WriteLine("got app");
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
// Do things with app
While Outlook is running, I expect to see:
app
got app
which occurs if a non-Outlook window set to the foreground (i.e. not an Explorer or an Inspector window) since Outlook has started. If Outlook is running and has always been in the foreground, I instead see:
app
Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
As a shot in the dark, I've tried to "boot up" Interop by doing the following when I notice this occurring:
bootApplication = new Microsoft.Office.Interop.Outlook.Application();
To no effect. Any input would be valued. Thank you!
Don't use GetActiveObject
with Outlook - it is a singleton, so creating a new instance of the Outlook.Application
object will return a pointer to the existing instance if it is already running.
User contributions licensed under CC BY-SA 3.0