jump to navigation

Outlook 2003 Add-in and ActiveSync July 5, 2006

Posted by codinglifestyle in C#, Office.
Tags: , , ,
trackback

Recently I found myself doing emergency maintenance on an Outlook 2003 add-in.  The add-in did little more than add a button to the standard toolbar when certain conditions were met.  One issue I discovered purely by accident was the effect on ActiveSync on such an add-in.  I discovered this when I shut down Outlook in-between debugging sessions and plugged in my Windows mobile phone for a recharge.  When I brought Outlook back up my add-in was nowhere to be found.  The issue is when another program, such as ActiveSync, initializes a headless instance of Outlook via COM (aka no GUI).  The result is the add-in is already loaded in memory from the COM instance so initialization which was looking for some GUI component, in my case a toolbar, fails.  It is necessary to reevaluate our initialization code in order to handle the case of these GUI-less instances of Outlook.  Most standard examples show adding toolbar buttons during the OnConnection event.  If a headless COM instance of Outlook is running ActiveExplorer will return null and most likely the initialization will fail.  As this event is only fired once, subsequent instances will never be initialized.  In order to catch new instances of Outlook we need to register for the NewExplorer event in OnConnection.  Then, in case this first instance is the Outlook we’re looking for try our initialization.  Last, when catching new explorers attempt to initialize those as well.  The end result is initialization is run for every instance of Outlook and you can guarantee your add-in will be initialized.

  public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
  {
   m_appObj = (Microsoft.Office.Interop.Outlook.Application)application;
   m_addInInstance = addInInst;
   Trace.WriteLine(“Outlook add-in: OnConnection”);

   try
   {
    Outlook.ExplorersClass explorers = (Outlook.ExplorersClass )m_appObj.Explorers;
    explorers.NewExplorer +=new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(explorers_NewExplorer);
    m_appObj.Startup +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_StartupEventHandler(m_appObj_Startup);

    //Try to initialize
    InitAddIn(m_appObj.ActiveExplorer());
   }
   catch (Exception ex)
   {
    Trace.WriteLine(“Outlook add-in error: ” + ex.Message);
   }
  }

  private void explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer)
  {
   Explorer.Activate();
   InitAddIn(Explorer);
  }

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: