设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用VBA代码处理菜单和工具栏之四

1970-1-1 08:00| 发布者: zhengjialon『文章』| 查看: 2508| 评论: 0

 

                          -------------------郑家龙编绎于MS OFFICE开发文档

复制菜单和工具栏

 

必需用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 中的“显示弹出式菜单栏”索引。

最新评论

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-9 00:48 , Processed in 0.078680 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部