Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't obtain Outlook.Application while Outlook is running until a non-Outlook window is in the foreground

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!

like image 470
Shroom Avatar asked Feb 07 '19 22:02

Shroom


People also ask

Why is my Outlook app not opening?

Make sure you're using the correct server settings that your IT administrator or ISP has provided. Configure Outlook using a POP3 or IMAP account instead of an Exchange account. If Outlook keeps crashing on your Android or iOS device, clear the browser's cache. Remove the Outlook app and reinstall it.

How do I fix Outlook not working in Windows 10?

If Outlook stops responding at a screen that says "Processing," you can close Outlook, start it in safe mode, then close it and open it normally to fix the problem. Close Outlook. Launch Outlook in safe mode by choosing one of the following options. In Windows 10, choose Start, type Outlook.exe /safe, and press Enter.


1 Answers

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.

like image 157
Dmitry Streblechenko Avatar answered Sep 27 '22 23:09

Dmitry Streblechenko