We are writing a contact syncing application for Outlook using .Net 3.0. We’re using “Microsoft Outlook 12.0 Object Library” or Microsoft.Office.Interop.Outlook;
We notice when we call:
ApplicationClass app = new Microsoft.Office.Interop.Outlook.ApplicationClass();
It will start Outlook if the user does not have it open. But if the user closes Outlook while our application is open, we get a COMException the next time we reference the Outlook ApplicationClass instance.
How can we compensate for this behavior? We do not want to re-open Outlook if we get this exception.
We would rather Outlook not start up at all, or be hidden, but we want the user to be able to use Outlook even if our application is open.
We could also somehow hook into an ApplicationExit event or something, but if we create a new ApplicationClass at this point, would it start Outlook again?
How can we fix this?
I agree with Adam, but instead of catching the exception you should hook into the process.exit event, and then recreate your object. This will start a new instance of outlook. That’s just how it works. Or you could run your sync only when the user has started Outlook.
If there’s an exchange server involved you could look into API’s against exchange instead.