Get Running Instance of Outlook (Code)
Posted: 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:
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:
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.
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);
mi.Display();
}
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");
else
ol = new Outlook.Application();
return ol;
}
// do something with ol
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.