Search code examples
c#vstoms-officeadd-in

custom Ribbon is loaded before ThisAddIn_Startup, but I'd like to initialize some things there - best solution?


I'm developing a Word Add-In with its own Ribbon tab. Most of my actual code is contain in some Service classes that get initialized in ThisAddIn_Startup and assigned to properties of the ThisAddIn singleton object. I also have an event listener that listens for clipboard events that I'd like to expose to the rest of the code as a property of the same object.

The problem that I'm running into is that the Load event of my Ribbon fires before the Startup event of ThisAddIn (even though the singleton object has already been initialized and assigned to Globals.ThisAddIn). In my Ribbon's Load event handler I would like to install a handler on that listener and also use one of those services. I can't, though, because at this point in program execution they're null.

What is the best solution to this issue? I could just make all of these things static members of ThisAddIn and initialize them in a static constructor, but that seems wrong and also destroys the whole point of having a Startup event (or a singleton object).

Oddly, I originally didn't have this problem when my Ribbon was just adding its components to the built-in Add-In tab - then the Load event fired after Startup.

(Note: I am doing all of this in Visual Studio 2012 and using the Ribbon designer.)


Solution

  • For this purpose i'm overriding BeginInit method. From my experience events occure in order (ribbon/initialization related):

    1. CreateRibbonExtensibilityObject
    2. BeginInit
    3. GetCustomUI
    4. Ribbon_Load
    5. ThisAddIn_Startup