Variatus
Variatus

Reputation: 14383

Can't make CommandBar visible

I have this code to create a temporary command bar in Word (2010). Same result in Excel. It creates the bar. I know because I can access it, read its properties, and delete it. But I can't make it visible. It should be floating somewhere near the top of my page, but it doesn't. Any ideas?

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

As a side question, MenuBar:=True is supposed to replace the existing with the added. It doesn't. Several instances of the bar are created. They can be accessed and deleted, until all are gone.

Upvotes: 1

Views: 1718

Answers (2)

Black cat
Black cat

Reputation: 6271

The problem is with the Position:= parameter. It is not axcepted with the Ribbon. If it is left then the controls appear in the Add-in tab.

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166735

Since the introduction of the Ribbon menu in Office 2007, the only place you can show a toolbar is on the "Add-ins" ribbon tab.

Upvotes: 3

Related Questions