在 Excel 中禁用功能区按钮

Disable Ribbon button in Excel

我试图做到这一点,以便根据 Excel 功能区上另一个自定义按钮的切换状态禁用自定义按钮。我在我的功能区 XML 中定义了一个按钮,如下所示:

<button id="ButtonCalc" label="Re-Calculate" size="large" onAction="calc" image="Calculate" getEnabled="disableRefresh"/>

getEnabled 标签的回调是:

'Callback for ButtonCalc getEnabled
Sub disableRefresh(control As IRibbonControl, ByRef enabled)

If calcState Then

    enabled = True

Else
    enabled = False

End If
End Sub

术语"calcstate"是由切换按钮控制的布尔运算:

<toggleButton id="ButtonMode" label="Light Mode" size="large" onAction="ToggleCalcClicked" image="Mode" getPressed="checkLight"/>

VBA 这部分:

Public calcState As Boolean

Dim MyRibbon As IRibbonUI

Sub ToggleCalcClicked(control As IRibbonControl, pressed As Boolean)

calcState = Not calcState
Worksheets("Setup").Range("L47").Value = calcState

Call ChangeCalcState

End Sub

一切正常,除了重新计算按钮被永久禁用。我做错了什么?

您必须使控件无效才能更改可见性或启用的属性。

Public calcState As Boolean

Dim MyRibbon As IRibbonUI

Sub ToggleCalcClicked(control As IRibbonControl, pressed As Boolean)

  calcState = Not calcState
  Worksheets("Setup").Range("L47").Value = calcState

  ChangeCalcState
  MyRibbon.InvalidateControl("ButtonCalc")

End Sub