If you are automating Outlook in a standalone application or other app outside of Outlook and have trouble on some machines with your CreateObject call succeeding when Outlook is not already running, this article may be for you.
Have you ever called someone only to be put on hold as soon as they answer? Do you hang up immediately, or do you hold on for a few minutes to see if they pick up? If you’ve ever ordered a pizza where I order mine, I know you’ve experienced this. Well, it turns out, Outlook [or any Office application for that matter] acts just like this rude operator. When you try to automate Outlook, the Outlook.exe process has to be running in order to serve your requests. Outlook is what’s called a COM server. So your call to CreateObject (or "new”) makes a call into the COM subsystem which does the work of spinning up an instance of Outlook for you to work from using the –Embedding switch which tells Outlook not to display any UI by default. Part of Outlook’s start up logic involves loading all the COM add-ins and calling into the OnConnect and OnStartupComplete events. This is where things could go badly for you.
If your user is an comaddinophile they may have some add-ins installed that take a looooooong time to load. They may be calling into a database or a webservice or just have really poor code that takes a long time to execute. Outlook does all this work on its main thread – they aren’t spun off to a background thread – so all the time it takes for these add-ins to load and run is time the COM subsystem is spending growing very impatient. It can, after a while, just time out or it could succeed the initial load, but if Outlook is busy handling other requests your call to get the Application dispatch object could be actively refused because Outlook is blocking. In these cases, you could end up with a RPC_E_SERVERCALL_RETRYLATER error bubbling up in your .net automation code. The error you get will be something like “Call was rejected by callee.”
Oftentimes, you can follow up your failed CreateObject call with a second one and it succeeds because at this time Outlook.exe is running and may be unblocked and ready to service your request. However, it may just block again. In these cases, you will need to implement an IMessageFilter to be able to tell the COM subsystem that you’re ok waiting and to keep retrying your call until it succeeds or you get a different error.
The best sample I found for this was describing implementing this to solve this same problem when doing Visual Studio automation, but the same logic applies. Andrew Whitechapel wrote a sample also. In his case he was describing a similar problem when you try to make object model calls on a background thread from an add-in. Here’s a VB.NET sample I wrote.
To use it, just drop that in your code and then wrap your automation code like this:
The important part to notice is the calls to MessageFilter.Register and MessageFilter.Revoke.
Starting in Silverlight 3, the algorithm that SL uses to determine the hosting URL of the SL control doesn’t work properly when the page is viewed as part of an Outlook Folder Home Page. This is actually a known issue and is being looked at for Silverlight 5 but was not discovered early enough to make it into Silverlight 4.
If you are developing a solution that includes the use of Outlook Folder Home Pages, I’d recommend you consider another option if you plan on doing any advanced things in your solution. Folder homepages are good for hosting plain ol’ HTML and an occasional Outlook View Control, but because of the security lock-downs that are added in each new version of Outlook, the flexibility you have for using various techniques inside OFHPs is limited. Depending on what you are trying to accomplish, you may want to consider using Form Regions and Ribbon customization or else simply providing a link to your high-complexity web page instead of providing the content inside Outlook itself.
The Office team put out some Channel9 videos and some tools to help you get your add-ins and applications onto the Office 2010 platform.