3

Is it possible to set an excel 2010 built in RibbonButton to enabled=false from an excel VSTO Add-In?

I tried the following:

CommandBarControls controlls=Globals.ThisAddIn.Application.CommandBars.FindControls(MsoControlType.msoControlButton, 7374, null, false);
/// 7374 is the office control id of the control I want to access

foreach (CommandBarControl control in controlls)
{
    control.Enabled = false;
}

But this seems to work only for the right click context menu. And not for the ribbon buttons.

sachleen
  • 30,730
  • 8
  • 78
  • 73
flosk8
  • 465
  • 1
  • 6
  • 17

2 Answers2

4

You can only disable tabs, not controls unless you use the startFromScratch Ribbon UI attribute. See MSDN for reference.

Also see Ribbon XML FAQ for good resources on Excel Ribbon manipulation.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
2

Not sure whether this would help you, but for a custom ribbon, you implement the getEnabled callback with a Ribbon XML.

In the XML:

<button id="btnMyButton" ... getEnabled="OnMyButton_GetEnabled" onAction="..."/>

In the code-behind:

public bool OnMyButton_GetEnabled(Office.IRibbonControl rControl)
{
  // return true or false to enable or disable 
}

You need to call the IRibbonUI.Invalidate() method if you need to forcefully call these callbacks (for example, when your enable/disable state variables are set due to some other event).

Btw, the Ribbon Designer interface (on VS 2010) doesn't seem to offer the ability to implement the getEnabled callback.

Ishan De Silva
  • 935
  • 2
  • 8
  • 22