会员登录 - 用户注册 - 网站地图 Office中国(office-cn.net),专业Office论坛
当前位置:主页 > 技巧 > Access技巧 > 宏/菜单/数据页 > 正文

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

时间:2004-08-13 12:36 来源:ACCESS中国/Office中国 作者:zhengjia… 阅读:

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

获取菜单和工具栏的信息

 

在所有的OFFICE组件应用程序中都包含有内置的工具栏,并且可以进行进一步的自定义显示相关命令,它们一共有三种类型:菜单栏,工具栏和弹出式菜单栏(本主题以工具栏代表三种类型来描述)。

你可以用下面的一段程序在立即窗口中显示它们的所有的工具栏以及工具栏上所包含的命令控件:

Function CBPrintCBarInfo(strCBarName As String) As Variant

    ' This procedure prints (to the Debug window) information

    ' about the command bar specified in the strCBarName argument

    ' and information about each control on that command bar.

   

    Dim cbrBar                      As CommandBar

    Dim ctlCBarControl              As CommandBarControl

    Const ERR_INVALID_CMDBARNAME    As Long = 5

   

    On Error GoTo CBPrintCBarInfo_Err

   

    Set cbrBar = Application.CommandBars(strCBarName)

   

    Debug.Print "CommandBar: " & cbrBar.Name & vbTab & "(" _

        & CBGetCBType(cbrBar) & ")" & vbTab & "(" _

        & IIf(cbrBar.BuiltIn, "Built-in", "Custom") & ")"

    For Each ctlCBarControl In cbrBar.Controls

        Debug.Print vbTab & ctlCBarControl.Caption & vbTab & "(" _

            & CBGetCBCtlType(ctlCBarControl) & ")"

    Next ctlCBarControl

 

CBPrintCBarInfo_End:

    Exit Function

CBPrintCBarInfo_Err:

    Select Case Err.Number

        Case ERR_INVALID_CMDBARNAME

            CBPrintCBarInfo = "'" & strCBarName & _

                "' is not a valid command bar name!"

        Case Else

            CBPrintCBarInfo = "Error: " & Err.Number _

                & " - " & Err.Description

    End Select

    Resume CBPrintCBarInfo_End

End Function

 

你可以在accessVisual Basic编辑器中的立即窗口中运行这个函数得到access所有的菜单或工具栏的命令按钮,例如,可以在立即窗口中键入以下命令:

? CBPrintCBarInfo(“Web”)

 

回车后你能在立即窗口中显示出Web工具栏所包含的所有命令集合,如下图:

 

 

如果命令按钮的类型显示为“Popup”时, 例如“收藏夹”这个命令它本身就是一个弹出式工具栏,你同样可以用本函数来得到它个每一个命令按钮的清单,如:

? CBPrintCBarInfo(“favorites”)

 

你可以用名称或集合索引来访问工具栏集合的每个工具栏,注意与其它集合不同的是所有集合索引都是从1开始的。

(责任编辑:admin)

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价: