无法使CommandBar可见

时间:2017-10-12 02:55:39

标签: vba excel-vba word-vba excel

我有这个代码在Word(2010)中创建一个临时命令栏。 Excel中的结果相同。它创造了酒吧。我知道因为我可以访问它,读取它的属性并删除它。但我不能让它可见。它应该漂浮在我页面顶部附近,但它不会。有什么想法吗?

Sub AddCommandbar()
    Const CmdName As String = "Test Bar"

    Dim MyBar As CommandBar
    Dim MyCtl As CommandBarControl
    Dim MyList() As String
    Dim Cmd As CommandBar
    Dim i As Integer

    ' delete the existing
    For Each Cmd In CommandBars
        If Cmd.Name = CmdName Then
            Cmd.Delete
            Debug.Print CmdName; " deleted"
            Exit For
        End If
    Next Cmd

    Exit Sub
    Set MyBar = CommandBars.Add(Name:=CmdName, _
                                Position:=msoBarFloating, _
                                MenuBar:=True, _
                                Temporary:=True)
    Set MyCtl = CommandBars(CmdName).Controls.Add( _
                                Type:=msoControlDropdown, _
                                Before:=1)
    MyList = Split("One,Two,Three", ",")
    With MyCtl
        .Caption = "Select a number"
        .Style = msoComboLabel
        .BeginGroup = True
        For i = 0 To UBound(MyList)
            .AddItem MyList(i)
        Next i
        .ListIndex = 1
    End With
    CommandBars(CmdName).Visible = True
End Sub

作为一个附带问题,MenuBar:=True应该用添加的替换现有的。它没有。创建了几个条形实例。可以访问和删除它们,直到所有内容都消失。

1 个答案:

答案 0 :(得分:3)

自Office 2007中引入功能区菜单以来,唯一可以显示工具栏的地方就是"加载项"功能区选项卡。