Copying a Command Bar Example

The following example copies a command bar and its controls to a new command bar. You enter the name of the command bar you want to copy and the name for the new command bar in the EnterNewName procedure. This procedure calls the general function CopyCommandBar, which uses the names you entered to create the new command bar.

Sub EnterNewName()
    CopyCommandBar "Menu Bar", "New Menu Bar"
End Sub

Function CopyCommandBar(strOrigCBName As String, _
        strNewCBName As String) As Boolean
    ' This procedure copies command bar named in
    ' strOrigCBName variable to new command bar
    ' named in strNewCBName variable.
    Dim cbrOriginal As CommandBar, intOrigCount As Integer
    Dim cbrCopy As CommandBar, cbrCtl As CommandBarControl

    On Error GoTo CBCopy_Err

    Set cbrOriginal = CommandBars(strOrigCBName)
    intOrigCount = cbrOriginal.Controls.Count
    Set cbrCopy = CommandBars.Add(strNewCBName)
    ' Make sure new command bar is visible.
    cbrCopy.Visible = True

    For Each cbrCtl In cbrOriginal.Controls
        cbrCtl.Copy cbrCopy
    Next cbrCtl
Exit Function

CBCopy_Err:
    MsgBox Err.Description
    Exit Function
End Function