无法在Excel工作区上隐藏自定义控件

时间:2017-10-11 19:08:48

标签: excel-vba ribbon-control vba excel

一直试图在excel功能区上隐藏自定义控件,我的代码一直显示来自我的RefreshRibbon()子的消息。无论我从HideImport()子传递什么字符串,控件仍然可见。我在这里包含自定义功能区的XML和问题的宏。

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="false">
    <tabs>
        <tab id="tabControl1" label="Control">
            <group id="grpImport1" label="Import" getVisible="GetVisible" tag="ShowFalse">
                <button id="btnDetail" label="Read Detail" image="Ex" size="large" onAction="ReadDetail" />
            </group>
            <group id="grpJourneys1" label="Journeys" getVisible="GetVisible" tag="ShowTrue">
                <button id="btnLegAdd" label="Add" image="AddLeg" size="large" onAction="AddLeg" />
                <button id="btnLegRemove" label="Remove" image="RemoveLeg" size="large" onAction="RemoveLeg" />
                <button id="btnLegRetime" label="Retime" image="RetimeLeg" size="large" onAction="RetimeLeg" />
            </group>
        </tab>
    </tabs>
</ribbon>

Dim ribControl   As IRibbonUI
Public strTag    As String

Sub RibbonOnLoad(ribbon As IRibbonUI)
   Set ribControl = ribbon
End Sub

Sub GetVisible(control As IRibbonControl, ByRef visible)
  If strTag = "Show" Then
    visible = True
  Else
    If control.Tag Like strTag Then
      visible = True
    Else
      visible = False
    End If
  End If
End Sub

Sub RefreshRibbon(Tag As String)
  strTag = Tag
  If ribControl Is Nothing Then
    MsgBox "Error, Save/Restart your workbook"
  Else
    ribControl.Invalidate
  End If
End Sub

Sub ReadDetail(control As IRibbonControl)
  MsgBox "This is Read Detail"
End Sub

Sub AddLeg(control As IRibbonControl)
  MsgBox "This is Add Leg"
End Sub

Sub RemoveLeg(control As IRibbonControl)
  MsgBox "This is Remove Leg"
End Sub

Sub RetimeLeg(control As IRibbonControl)
  MsgBox "This is Retime Leg"
End Sub

Sub HideImport()
  Call RefreshRibbon(Tag:="*True")
End Sub

在阅读你对其他问题的回答之后,通常不会自己在这里发布,但这让我很生气。

1 个答案:

答案 0 :(得分:0)

您的自定义功能区代码中没有回调函数: 示例:<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonOnLoad">

所以你没有运行RibbonOnLoad()程序。因此,您没有设置变量ribControl