End Outlook process as soon as the process is closed programatically?

3

ProblemRootCause: Unable to get the SelectNamesDialog to display over the GUI when the Outlook application is running.

How I tried to solve:

I tried triggering an Outlook event by opening and closing a Mailitem and then display the SelectNamesDialog. Now it appears over our GUI.

Issue currently faced:

Opening and closing Mailitem before the SelectNamesDialog displays should be done only if the Outlook application is running. If it is not running, then SelectNamesDialog can be directly displayed.

if (msOutlook == true)
{
    Microsoft.Office.Interop.Outlook.MailItem oMailItem = (Microsoft.Office.Interop.Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    oMailItem.Display(false);
    oMailItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
}

bool isDisplayed = SelectNamesDialog .Display();
Microsoft.Office.Interop.Outlook.Recipients recipients = SelectNamesDialog .Recipients;

if (recipients.Count > 0)
{----

}

So in order to find out if the Outlook application is running, I use Process.GetProcesses(). msOutlook is true when the process is running.

If Outlook is not running and the display button is clicked for the first time, msOutlook is false and snd.Display() will be executed and it works fine. But the Outlook process is not ending as soon as SelectNamesDialog is closed. The Outlook process will end only after some time has passed. So if the user immediately clicks the display button for the second time, even if the outlook application is not open in his system, the process will be 'running' and msOutlook will be true.

Now the below lines execution will throw a COMException

Microsoft.Office.Interop.Outlook.Recipients recipients = SelectNamesDialog .Recipients;

if (recipients.Count > 0)
{----
-----
}

Text:

System.Runtime.Interopservices.COMException
{"The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"}
c#
outlook
focus
asked on Stack Overflow Mar 18, 2014 by user3430189 • edited Mar 18, 2014 by Mighty Badaboom

2 Answers

1

The only way I've been able to work through this sort of thing is set up a timer and keep checking for Process().HasExited. GetProcesses cannot distinguish between processes that are closing themselves down, or in any state that is not perhaps valid to a user (or developer of a user interface). You're likely getting that exception because the Outlook object has managed to accomplish part of its shutdown procedure, i.e. disconnect from its clients, but just hasn't quite finished cleaning itself up and gone away.

Obviously your situation is complicated by the fact that you have a GUI with perhaps a user wanting to push a button. I'd recommend having that button be disabled until HasExited == true.

answered on Stack Overflow Mar 18, 2014 by ouflak
1

What exactly do you mean by " Unable to get the SelectNamesDialog to display over the GUI when the Outlook application is running.". Do you mean the dialog id displayed behind your app?

You can try to bring Outlook to the foreground (Application.ActiveWindow.Activate), but then you will need to bring your app back.

Or you can use Redemption and its RDOSelectNamesDialog - since Redemption is running in-proc without using Outlook at all, the dialog box will be modal to your app. You can also explicitly set the RDOSession.ParentWindow property to ensure that a particular window is the parent of the address book dialog.


User contributions licensed under CC BY-SA 3.0