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 e开发者_StackOverflow社区ven 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.
You won't be able to use the Outlook (or any Office) COM-interop libraries without the host application running, and (to the best of my knowledge) Outlook does not offer the ability to run multiple instances, so creating your own instance that's hidden probably isn't an option.
My only suggestion would be to catch the exception, present the user with a warning dialog not to close Outlook, then reopen it gracefully. Obviously this isn't the solution you were looking for, but I don't see a way around forcing Outlook to say open.
If your outlook user is an exchange user you could also sync with exchange instead (ok it depends on what you are doing). There are many options there for connectivity, Dependant on with version: Exchange Web Services, or DAV or via Mapi could be ways to go.
精彩评论