打印本文 打印本文  关闭窗口 关闭窗口  
用VBA代码处理菜单和工具栏之四
作者:佚名  文章来源:不详  点击数  更新时间:2008/4/18 14:54:47  文章录入:杜斌  责任编辑:杜斌

复制菜单和工具栏

  必需用VBA代码才能复制现有的工具栏。你可以用Add方法创建一个和你要拷贝相同类型的工具栏,然后再用CommandBarControl对的Copy方法将源工具栏上的每一个命令按钮复制到新工具栏上,下面的函数将是以这种方法实现复制工具栏:

strOrigCBName参数是指被复制的源工具栏,strNewCBName参数指新工具栏的名称,可选参数blnShowBar决定了新工具栏是否显示出来。

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

注意:

    1.这个函数的strNewCBName参数不能和现有工具栏中同名;

    2.如果你复制一个弹出式菜单栏,并且设blnShowBar参数为TRUE,当运行这个函数时,这个弹出式菜单栏将显示在当前鼠标的位置,更多的关于显示弹出式菜单栏的信息和描述请参阅Microsoft Office Visual Basic Reference Help 中的“显示弹出式菜单栏”索引。

打印本文 打印本文  关闭窗口 关闭窗口