Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 源码示例

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

时 间:2004-06-21 00:00:00
作 者:郑家龙    ID:90  城市:上海
摘 要:用VBA代码处理菜单和工具栏之四 
正 文:

复制菜单和工具栏

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



Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助