How to disable Excel built-in RibbonButton?

99封情书 提交于 2019-12-07 15:24:18

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/9619256/how-to-disable-excel-built-in-ribbonbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!