how to get enable the ribbon button based on the cell selection?

Category: visual studio officetools


Roopini on Thu, 13 Mar 2014 08:27:42

I have developed an addin where I have used the Ribbon xml to create. I have a method called activateMenu in MyApplication.cs class

private void ActivateMenu(int mode, bool active)

             int l_mode = mode;

             if (l_mode >= PluginConstants.MENU_MODULE)
                // m_cbbVisModule3D.Enabled = active;
                // m_cbbVisModule3DTool.Enabled = active;
                 l_mode -= PluginConstants.MENU_MODULE;

Earlier i used the old commandbar method for the button and its control to enable. I want to use the same kind in the new VSTO implementation. How can i achieve this. I have used a callback method but that is still not working. I want to use it in this method to enable and disable the button and its control. I want to enable and disable the button based on the cell selection in the tab and the contextmenu.



Eugene Astafiev on Thu, 13 Mar 2014 13:13:29

Hello Roopini,

Starting from Office 2007 the Fluent UI is used for most of Office applications:

Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Also you may find the following articles in MSDN library helpful:

How to: Customize the Ribbon

Programmatically Customizing the 2007 Office Fluent User Interface

At runtime you can use the Invalidate method of the IRibbonUI interface for refreshing your custom UI depending the cell selection.

Roopini on Fri, 14 Mar 2014 01:57:17

Thanks, but i have three columns say (Employee, Dept, Number). When these cell are selected,

the button should enable on its own. If Employee cell is selected, the first ribbon button should automatically enable and rest of the button should be disabled. If Dept is selected, first button to be disable, second should be enabled and third should be disabled. Something like this?

How can I do that? I have also tried the getEnable property. But I want to do this based on the condition of Employee,dept and number selected. Where these can be placed anywhere in the sheet.

Marvin_Guo on Fri, 14 Mar 2014 10:21:54


Based on my understanding, you need to custom ribbon to enable and disable the button when user selected specified cells.

You can create ribbon xml item defines a button control, and it has a getEnabled=”doSomething” callback.

In your “ThisAddIn” class, you need handle the Worksheet.SelectionChange Event to change enable property of Ribbon class, and then invoking IRibbonUI.Invalidate method to update all of the controls of the Ribbon user interface.

I wrote a sample for your reference:

In Ribbon XML file:


<customUI xmlns="" onLoad="Ribbon_Load">



      <tab idMso="TabAddIns">

        <group id="MyGroup"

               label="My Group">

          <button id ="Mybutton " label ="mybutton" getEnabled ="SetEnabled"/>






In Ribbon.cs:

 public bool IsEnabled = true ;

        //CallBack function

        public bool SetEnabled(Office.IRibbonControl control)


            return IsEnabled;


        public void ButtonEnabled()


            IsEnabled = false;



Thus, you could call ButtonEnabled method in your SelectionChange event handler.


We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.

Roopini on Mon, 17 Mar 2014 02:50:42


But i need to enable and disable it but not visible or invisible.

Eugene Astafiev on Mon, 17 Mar 2014 18:41:57

Hello Roopini,

The code is identical for the getEnabled callback. Did you have a chance to look at the articles I mentioned in my previous post there?

Anyway, you may find the Ribbon Controls Sample project in C# and VB.NET helpful.

Roopini on Tue, 18 Mar 2014 02:22:42

Hi Eugene,

Yes I checked, but I wanted to know how to pass control as parameter when called from a 

different method.

Eugene Astafiev on Tue, 18 Mar 2014 05:53:45

Hi Roopini,

Callbacks are designed to be called by Office (Ribbon Extensibility). You shouldn't call them directly. Instead, you can set a local variable (for example, it cab be a Boolean) which indicates whether a control is enabled or not  The variable can be changed in any method. Then, when you need to update state immediately, you can call the Invalidate method of the IRibbonUI interface:

<customUI … onLoad=”MyAddInInitialize” …>
Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
    Set MyRibbon = Ribbon
End Sub

Sub myFunction()
    ‘ Invalidates the caches of all of this add-in’s controls 
End Sub

Callbacks will be invoked then. In the code of your getEnabled callback you can return the boolean value you set before in another method.

Roopini on Tue, 18 Mar 2014 06:20:33

Hi Eugene,

It is same as that of how Marvin has given the example ?

Eugene Astafiev on Tue, 18 Mar 2014 06:35:21

Yes, it is.