Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) when running through Windows Service?

3

i am try to hook to outlook application from windows Service but getting an exception Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) here is my code.

   public void ItemSendEvent()
    {
        try
        {
           if (Process.GetProcessesByName(ApplicationConstants.OUTLOOK_PROCESS_NAME).Count() > 0)
                {
                    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                    outlookApplication = Marshal.GetActiveObject(ApplicationConstants.OUTLOOK_APPLICATION_NAME) as Microsoft.Office.Interop.Outlook.Application;
                    Microsoft.Office.Interop.Outlook.NameSpace nameSpace = outlookApplication.GetNamespace(ApplicationConstants.OUTLOOK_NAME_SPACE);
                    nameSpace.Logon("", "", Missing.Value, Missing.Value);
                    nameSpace = null;
                    outlookApplication.ItemSend += outlookApplication_ItemSend;
                }
                log.Info("Outlook Item Send event registered successfully.");
        }
        catch (System.Exception ex)
        {
            log.Error("Exception occurred while registering Outlook Item Send event. " + ex.Message);
        }
    }

but the same code when i launch it through Windows Form Application its working Fine. i gone through some site's and they were saying that outlook object is not in ROT Table. what will be the solution.

c#
.net
outlook
windows-services
ms-office
asked on Stack Overflow Jun 2, 2016 by Ravi Kanth

4 Answers

5

Outlook, or any other Office app, cannot run in a Windows service even if your service is running as an interactive user. Only Extended MAPI (C++ or Delphi only) or an Extended MAPI wrapper like Redemption (its RDO family objects) can be used in a service.

In your particular case, it looks like you are trying to trap the Application.ItemSend event. There is absolutely no reason to create a Windows service for that. Create a COM addin - it will be loaded by Outlook and run as long as Outlook itself is running in the same process in the same security context.

answered on Stack Overflow Jun 2, 2016 by Dmitry Streblechenko • edited Nov 27, 2020 by Dmitry Streblechenko
2

Two common issues could cause this.

The first would be that you are running Visual Studio in Administrator mode and you are starting your program from within VS, and the Office application is not. To fix that, you need to run your Office application with elevated privileges, in Administrator mode, as well.

The second could be caused by the application not being fully started/loaded when you call Marshal.GetActiveObject(...).

answered on Stack Overflow Jun 2, 2016 by gmiley
0

you don't need to have your application as a service to get it on the background ...

if your winform work well, just put your winform in background running on the systray for instance

answered on Stack Overflow Nov 8, 2016 by bobzer
0

Old but still significant thread.

I colide with this error trying to access Outlook data using the MS example.

Treating the error in a Try / Catch block and offering the option of newing Outlook solves the problem:

const int ERROR_HRESULT_0x800401E3_MK_E_UNAVAILABLE= -2147221021;
        Outlook.Application application = null;
        // Check whether there is an Outlook process running.
        if (Process.GetProcessesByName("OUTLOOK").Any())
        {
            try
            {
                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                }
            }
            catch (Exception ex)
            {
                //This is the branch where you can get correctly the current Outlook instance
                if (ex.HResult == ERROR_HRESULT_0x800401E3_MK_E_UNAVAILABLE)
                {
                    application = new Outlook.Application();
                }
            }
        }
        else
        {
            application = new Outlook.Application();
        }

Although the newing trick functions, no other Outlook instance is created, as Outlook behaves like a Singleton.

I only tested it with Office 365 64 bits installed.

answered on Stack Overflow Nov 15, 2020 by Marcelo Scofano Diniz • edited Dec 26, 2020 by Marcelo Scofano Diniz

User contributions licensed under CC BY-SA 3.0