How ActiveSync can affect your Outlook Add-ins

If you develop / use Outlook Add-ins for quite some time you'll run into this fairly common issue with ActiveSync (or any other application that uses Outlook Automation). Users who use PDAs / SmartPhones that run Microsoft Windows Mobile use ActiveSync to synchronize their Outlook information with the mobile device. ActiveSync for this reason starts up Outlook using Outlook Automation (in pure dev language - creates an Outlook.Application object) - Outlook being a Singleton application (only one thread of Outlook.exe runs at any given point in time) if Outlook.exe is already running, ActiveSync uses the current running thread to fetch the Outlook info required to sync with the mobile device.

However, if Outlook isn’t running when ActiveSync is started then ActiveSync creates a new Outlook.exe process - It still does load your Add-ins, however, without the Outlook UI (This is true for all versions of Outlook XP SP3 and above - see https://support.microsoft.com/?id=329098). Since no UI is present the Explorer count at this point would be Zero and a call to the ActiveExplorer() method of the Application class would return a null.

This is fairly important and something that you should always keep in mind when you develop an Outlook Add-in. Always check if the Explorer count is greater than Zero before you make a call to ActiveExplorer. If the count is indeed Zero (which means Outlook has started by Outlook Automation and not using the UI) handle your application gracefully.

A fairly common scenario where developers hit upon is that they try to add buttons to a toolbar on the Explorer - which don’t seem to appear when ActiveSync is started before Outlook. This is because most of the time developers try to add buttons using the ActiveExplorer in the OnConnection event. Since the ActiveExplorer will return a null when there is no Outlook UI - no buttons are added (this can also lead someone to believe that the Addin is not loading though it’s not in the Disabled Items either).

The way you can fix this is by not adding the buttons to the Explorer in the OnConnection event but instead to trap the NewExplorer event, get hold of the Explorer object in this event handler and wire up its Activate event and then add the buttons in the Activate Event instead. Additionally, note that the Activate event fires every time your Explorer object receives focus - so you must check if the buttons already exist on the Explorer before you add them to avoid the buttons being added multiple times.