动态控制VBA功能区

时间:2015-10-23 08:09:12

标签: excel vba excel-vba ribbon

我有一个应用程序,我第一次使用功能区。我可以填充自己的控件,并回调子程序没问题。我现在处于开发阶段,我希望隐藏/动态显示一些组以提供更好的用户体验。

我可以通过更改CallbackGetVisible中的visible属性来隐藏/显示工作簿加载期间的组,但是当我更改值并在应用程序运行时调用RefreshRibbon时,它会在Rib.Invalidate处中断。测试显示Rib(IRibbonUI)对象(Rib)设置为空。有没有办法让对象保持活跃状态​​,或者还有其他我缺少的东西?

XML

<!-- This is example : Custom tab for your favorite macros part 1   -->
<customUI onLoad="onLoad" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <!-- Add Custom tab to the ribbon with your favorite buttons-->
  <!-- The example add three groups to the new tab -->
  <!-- On the last tab there is a menu with five options-->
  <ribbon>
    <tabs>
      <tab id="MyCustomTab" label="MIS DASHBOARD" insertAfterMso="TabHome">
        <group id="customGroup1" label="Menus">
          <menu id="MyDropdownMenu1" label="Dashboard" size="large" imageMso="ChartTypeOtherInsertGallery">
            <button id="customButton1" label="Dashboard Filters" onAction="ShowfrmDashboardFilters" imageMso="ViewsLayoutView" />
          </menu>
          <menu id="MyDropdownMenu2" label="Reports" size="large" imageMso="SlideMasterChartPlaceholderInsert">
        <button id="customButton2" label="Hub Templates" onAction="ShowfrmReportsTemplates" imageMso="CreateTableTemplatesGallery" />
        <button id="customButton3" label="Enter Exceptions" onAction="ShowfrmReportsExceptions" imageMso="TableDesign" />
          </menu>
          <menu id="MyDropdownMenu3" label="Admin" size="large" imageMso="FileDocumentEncrypt">
            <button id="customButton8" label="Data Entry" onAction="ShowfrmAdminDataEntry" imageMso="TableDesign" />
            <button id="customButton10" label="Manage Business Priorities" onAction="ShowfrmAdminBP" imageMso="QueryShowTable" />
            <button id="customButton11" label="Manage Templates" onAction="ShowfrmAdminTemplates" imageMso="CreateTableTemplatesGallery" />
            <button id="customButton12" label="Manage Metric Templates" onAction="ShowfrmAdminMetrics" imageMso="AccessListAssets" />
          </menu>
      <menu id="StaffDatabaseMenu" label="Staff Database" size="large" imageMso="CreateTableTemplatesGallery">
            <button id="customButton50" label="Update Data" onAction="ShowfrmStaffDatabase" imageMso="CreateTableTemplatesGallery" />
        <button id="customButton51" label="Add" onAction="ShowfrmStaffDatabaseAdd" imageMso="MailMergeRecipientsEditList" />
        <button id="customButton52" label="Delete" onAction="ShowfrmStaffDatabaseDelete" imageMso="PageMenu" />
          </menu>
    <menu id="MyDropdownMenu10" label="Local Data" size="large" imageMso="CreateReportFromWizard">
            <button id="customButton60" label="EWB" onAction="ShowfrmEWB" imageMso="CreateReportFromWizard" />
          </menu>
        </group>
    <group id="customGroup4" label="Support">
        <button id="customButton20" label="Feedback" size="large" onAction="ShowFeedback" imageMso="FileSendMenu" />
        <button id="customButton21" label="Guidance" size="large" onAction="ShowGuidance" imageMso="TentativeAcceptInvitation" />
        <button id="customButton22" label="Change Control" size="large" onAction="ShowChangeControl" imageMso="ReviewDisplayForReview" />
    </group>
    <group id="customGroup5" label="Actions" getVisible="CallbackGetVisible"> 
        <button id="customButton30" label="Save" size="large" onAction="ShowGuidance" imageMso="ExportToVCardFile" />
        <button id="customButton31" label="Cancel" size="large" onAction="ShowGuidance" imageMso="OmsDelete" />
    </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

代码

Option Private Module
Option Explicit
Const errModule As String = "modRibbon"

'Module Variables
Dim Rib As IRibbonUI
Public MyTag As String

Sub CallbackGetVisible(control As IRibbonControl, ByRef visible)

    visible = True

End Sub

Sub RefreshRibbon()

    Debug.Print "RefreshRibbon"
    If Rib Is Nothing Then
        MsgBox "Error, Save/Restart your workbook"
    Else
        Rib.Invalidate
    End If
End Sub

'==================================================================================================================================================================
'Called On Load From XML
'==================================================================================================================================================================
Sub onLoad(ByVal ribbon As IRibbonUI)
On Error GoTo err_Handle
Const strError As String = "Error - Please Contact " & gblDeveloper & " Quoting 'OnLoad'"

    Set Rib = ribbon
    Rib.ActivateTab ("MyCustomTab")
    GoTo Cleanup

'Set Any Objects to Nothing, Exits Sub
Cleanup:
    Set Rib = Nothing
    Exit Sub
'Throw Error
err_Handle:
    errMsg strError & Chr(10) & Err.Description & Chr(10) & errModule
    Resume Cleanup
End Sub

修改

我认为Rib.Invalidate会导致CallbackGetVisible运行,因此会相应地设置Group的visible属性。但是当功能区为Nothing时,Invalidate将不会运行。

1 个答案:

答案 0 :(得分:3)

您将@设置为子Rib的{​​{1}}部分中的Nothing

删除该行,只要保存Cleanup:变量的代码模块打开,就可以使用onLoad对象。