Microsoft Office 2000/Visual Basic Programmer's Guide |
You must use VBA code to copy an existing command bar. You create a copy of a command bar by creating a new command bar of the same type as the one you want to copy, and then use the CommandBarControl object's Copy method to copy each control from the original command bar to the new command bar. The following procedure illustrates how to use VBA to copy an existing command bar:
Function CBCopyCommandBar(strOrigCBName As String, _
strNewCBName As String, _
Optional blnShowBar As Boolean = False) As Boolean
' This procedure copies the command bar named in the strOrigCBName
' argument to a new command bar specified in the strNewCBName argument.
Dim cbrOriginal As CommandBar
Dim cbrCopy As CommandBar
Dim ctlCBarControl As CommandBarControl
Dim lngBarType As Long
On Error GoTo CBCopy_Err
Set cbrOriginal = CommandBars(strOrigCBName)
lngBarType = cbrOriginal.Type
Select Case lngBarType
Case msoBarTypeMenuBar
Set cbrCopy = CommandBars.Add(Name:=strNewCBName, Position:=msoBarMenuBar)
Case msoBarTypePopup
Set cbrCopy = CommandBars.Add(Name:=strNewCBName, Position:=msoBarPopup)
Case Else
Set cbrCopy = CommandBars.Add(Name:=strNewCBName)
End Select
' Copy controls to new command bar.
For Each ctlCBarControl In cbrOriginal.Controls
ctlCBarControl.Copy cbrCopy
Next ctlCBarControl
' Show new command bar.
If blnShowBar = True Then
If cbrCopy.Type = msoBarTypePopup Then
cbrCopy.ShowPopup
Else
cbrCopy.Visible = True
End If
End If
CBCopyCommandBar = True
CBCopy_End:
Exit Function
CBCopy_Err:
CBCopyCommandBar = False
Resume CBCopy_End
End Function
The CBCopyCommandBar procedure is available in the modCommandBarCode module in CommandBarSamples.mdb in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the companion CD ROM.
Notes