views:

978

answers:

1

I have successfully used the Excel and Word addin templates in Visual studio 2008 to create a project that adds to the ribbon, but I am having difficulty with shared addins. I have created an add in that uses a xml file to modify and add to the ribbon and I can catch the events from buttons added, but I can't for the life of me figure out how to hide buttons or add controls once it is up and running. Basically I need some kind of reference to a ribbon and all I can get is a reference to an object that implements IRibbonUi which does not help. Does anyone have any good shared add-in examples or advice?

+2  A: 

There are two possible ways to control the visibility of your ribbon controls. You can either use the visible property or the getVisible event. Both approachs require modifying the xml file you are already using.

If this is your partial xml file now:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction"/>

Then you can hide the control by changing it to:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" visible="false"/>

This isn't much use as it is hardcoded. To get closer to what you are looking for change the xml to:

<button id="MyButton" label="Hello" onAction="MyButtonOnAction" getVisible="MyButtonGetVisible"/>

And then in the same way you have done the MyButtonOnAction callback you create the MyButtonGetVisible callback which happens to have this signature (C#):

bool MyButtonGetVisible(IRibbonControl control)

With this method you can then return true or false depending on whether you want to show/hide the button. The next question you might have is, the MyButtonGetVisible callback is only ever called once right after my add-in is loaded. I want to show/hide the button later on, how do I get the callback to trigger?

To accomplish this you can use that IRibbonUI object you have and call the Invalidate function which will cause your entire ribbon to be invalidated which will cause the callback to be called. If you require finer control you can call the InvalidateControl function which takes the ControlID as a parameter, and will only invalidate one control, which will cause the callback to be called.

As for adding controls on the fly, I don't believe that to be possible.

Further resources I would suggest: Part 1 Part 2 Part 3

vengafoo
thanks, that getVisible callback combined with the InvalidateControl call was exactly what we ended up doing. The only pain in the end is that when we overrode the open file button, the ctrl-o shortcut calls our code in word, but not in Excel. Best we can find on this is that the Excel team did not implement this.
Codezy