Get Running Instance of Outlook (Code)

Ask general questions here.
Posts: 1
Joined: Thu Apr 26, 2018 4:30 pm

Get Running Instance of Outlook (Code)

Post by K-Man » Thu Apr 26, 2018 11:59 pm

Hi all,

New to forum and fairly new to Ranorex.

I need to use the Office Interop library for Outlook, to inspect messages and attachments, etc. I'm having a problem getting a reference to the running instance of Outlook when running a test suite from Ranorex Studio (I don't want to have to start Outlook each time). Some simple code that works fine when I double-click the compiled .exe in bin\Debug, but doesn't work from Studio:

Code: Select all

private void GetOlCreateMail()
    Outlook.Application ol = new Outlook.Application();
    Outlook.MailItem mi = (Outlook.MailItem)ol.CreateItem(Outlook.OlItemType.olMailItem);
Running this from Studio, I get:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

Another idea is to first check whether Outlook is running and call Marshal.GetActiveObject(), like this:

Code: Select all

private Outlook.Application GetOutlookInstance()
    Outlook.Application ol = null;
    System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");
    int collCount = processes.Length;
    if (collCount != 0)
        ol = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
        ol = new Outlook.Application();
    return ol;
// do something with ol
Again, running this from the .exe directly works fine, but running from Studio produces:
Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

Research indicates this second error happens when Outlook and the calling code are running under different accounts or security contexts, but from what I can see in Task Manager, every process spawned during test execution is running under the same account, which is the same account running Outlook.

Any help appreciated.

Posts: 191
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: Get Running Instance of Outlook (Code)

Post by McTurtle » Wed May 02, 2018 3:20 pm

Hello K-Man,

There is a fairly long discussion regarding a similar issue on msdn: Failing to create an email with outlook interop when outlook has been opened by another user account

Is it possible that you are trying to create a new Outlook instance while another one is already running? If so, then this is exactly your error and you should start to research that msdn forum post.