dynamic控制VBA色带

我有一个应用程序,我第一次使用function区。 我可以填充我自己的控件,并callback子程序没有问题。 我现在正处于发展阶段,我希望能够dynamic地隐藏/显示某些组以提供更好的用户体验。

我可以通过更改CallbackGetVisible中的可见属性在工作簿加载过程中隐藏/显示组,但是当我更改该值并在应用程序运行时调用RefreshRibbon时,它将在Rib.Invalidate处中断。 testing显示,Rib(IRibbon)对象(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运行,因此,相应地设置组的可见性。 但是当Ribbon没有时,Invalidate将不会运行。

在“ Cleanup:中将“ Rib设置为“ NothingCleanup:子部分的一部分。

删除该行,只要保持Ribvariables的代码模块打开,就可以使用Rib对象。