Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 524
Alex Godofsky Avatar asked Apr 17 '13 19:04

Alex Godofsky


2 Answers

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
like image 67
tinamou Avatar answered Sep 20 '22 18:09

tinamou


I have tested the solution of tinamou, it doesn't work anymore. Ribbon_load indeed occurred after BeginInit but at this step the ribbon is fixed and read-only. There is no way to add groups buttons .. (it is however possible to modify dynamic menus).

I think the best way (stable) is to preload several groups etc.. and to use the visibility property of controls to obtain a kind of dynamic ribbon.

See also this post localize VSTO addin according to the language of the office product, it seems that CreateRibbonExtensibilityObject is triggered at random times.

like image 39
Malick Avatar answered Sep 16 '22 18:09

Malick