jive137
jive137

Reputation: 92

Inconsistent Outlook Addin Event Firing

On my development machine I have the FormInitializing and FormShowing events firing before RibbonLoad. I created a setup package in VS 2010 and installed on a vanilla Windows 7 Ultimate with Outlook 2010 installed.

The addin wasn't appearing on my meeting request form. So I setup remote debugger and to my astonishment the RibbonLoad is firing before the two form events mentioned above. A null exception is being throw b\c the code in the RibbonLoad relies on the FormRegion already being loaded. Can anyone offer any insight?

Upvotes: 1

Views: 562

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31651

There is no defined order for certain Outlook events - the Ribbon UI and the Inspector UI are completely different components, despite them both being display in the same window. The Outlook runtime may trigger Ribbon and Inspector events in different orders. It would be your job to synchronize the two events (RibbonLoad and FormInitializing) if you need some initialization done. You cannot assume that the ordering will always be the same.

I notice this same behavior when ThisAddIn.Startup fires before ThisAddIn.CreateRibbonExtensibilityObject, but sometimes after depending on how Outlook triggers the sequencing. You can just use a static variable with sync locking to ensure your initialization code is only triggered once.

Here is an example I used to synchronize the Startup event with the RibbonLoad event:

public partial class ThisAddIn
{
  static bool formInitialized = false;
  static readonly object padLock = new Object(); 

   private void ThisAddIn_Startup(object sender, System.EventArgs e)
   {
       lock(padLock) { if (!formInitialized ) { InitializeForm(); } }
       // startup code
    }

   private void InitializeForm() 
   {
      // init code
      formInitialized = true;
   }

   protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        lock(padLock) { if (!formInitialized) InitializeForm(); }
        // Create ribbon UI
    }
}

Upvotes: 1

Related Questions