简体   繁体   English

Excel 功能区宏未启用

[英]Excel Ribbon Macro not enabled

We have custom ribbons on many Excel workbooks.我们在许多 Excel 工作簿上都有定制色带。 Occasionally we experience an error message from Excel saying the '_getEnabled' macro is disabled (see image).有时我们会遇到来自 Excel 的错误消息,指出“_getEnabled”宏已禁用(见图)。 This message appears for every ribbon control that uses a macro to control the enabled property.对于使用宏来控制已启用属性的每个功能区控件,都会显示此消息。
宏被禁用错误消息

The xml in the ribbon is (some names are redacted):功能区中的 xml 为(部分名称已编辑):

<button id="name_btn" size="large" image="imagename" label="somelabel" getEnabled="VBA_####.######_getEnabled" onAction="VBA_####.######_onAction"/>

and the VBA is: VBA 是:

Sub ######_getEnabled(control As IRibbonControl, ByRef returnedVal)
    returnedVal = CheckPermissions("SomeName")
End Sub

Has anyone else seen this error?有没有其他人看到这个错误? The error messages appear when another workbook is the active workbook but Excel seems to be trying to refresh the ribbon controls for the workbook that contains the code.当另一个工作簿是活动工作簿但 Excel 似乎正在尝试刷新包含代码的工作簿的功能区控件时,会出现错误消息。 As mentioned earlier, this happens occasionally and we have not seen any identifiable pattern.如前所述,这种情况偶尔会发生,我们还没有看到任何可识别的模式。

You can replicate the behaviour if you set a breakpoint in one of the callbacks in VBA (GetImage, GelLabel, GetEnabled etc.) and when the code breaks you go and activate another workbook.如果您在 VBA(GetImage、GelLabel、GetEnabled 等)中的一个回调中设置断点,并且当代码中断 go 并激活另一个工作簿时,您可以复制该行为。 Finally, when you continue running the code (ex. press F5) the error will occur.最后,当您继续运行代码(例如按 F5)时,将发生错误。

You can force a ribbon refresh using the ribbon .Invalidate method (if you previously saved the ribbon object).您可以使用功能区.Invalidate方法强制刷新功能区(如果您之前保存了功能区对象)。 You can save the ribbon object by defining an onLoad callback:您可以通过定义onLoad回调来保存功能区 object:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="InitRibbon">

with this in VBA:在 VBA 中:

Private moduleLevelRibbonObject As IRibbonUI

Public Sub InitRibbon(ribbon As IRibbonUI)
    Set moduleLevelRibbonObject = ribbon
End Sub

Calling moduleLevelRibbonObject.Invalidate will refresh the ribbon (if state was not lost - but that is a different topic).调用moduleLevelRibbonObject.Invalidate将刷新功能区(如果 state 没有丢失 - 但这是一个不同的主题)。

Quick fix that I've found.我发现的快速修复。 Just add something like this:只需添加如下内容:

If Not ThisWorkbook Is ActiveWorkbook Then ThisWorkbook.Activate

to all your callbacks, so that when the callback returns to the caller, the correct workbook is active and the next callback is called correctly.到您的所有回调,以便当回调返回给调用者时,正确的工作簿处于活动状态并正确调用下一个回调。 Add the line preferably at the end of the method just before End Sub/Function .最好在End Sub/Function之前的方法末尾添加该行。 Example:例子:

Public Sub GetPressed(ctrl As IRibbonControl, ByRef isChecked As Variant)
    Select Case ctrl.ID
    Case "AdminMode"
        isChecked = IsAdminModeOn()
    Case Else
        Debug.Print "Toggle <" & ctrl.ID & "> does not have a return " _
            & "boolean for GetPressed!"
    End Select
    If Not ThisWorkbook Is ActiveWorkbook Then ThisWorkbook.Activate
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM