Excel菜单和工具栏概要(2)
4 命令栏对象(CommandBar对象)
CommandBar对象代表Excel中的一个命令栏,有一些常用的属性。
(1)BuiltIn属性。只读,代表该命令栏是否为内置命令栏,True为内置命令栏,False则不是。
(2)Enabled属性,设置是否隐藏命令栏。
(3)Left属性,以像素为单位返回命令栏控件相对于屏幕左边缘的距离。
(4)Name属性,返回命令栏的名称。
(5)Position属性,指定命令栏位置,可以为下列常量之一:
msoBarLeft—位于窗口左侧;
msoBarTop—位于窗口顶部;
msoBarRight—位于窗口右侧;
msoBarBottom—位于窗口底部;
msoBarFloating—浮动在屏幕中;
msoBarPopup—快捷菜单。
(6)Protection属性,指定保护命令栏的类型,可以为下列常量之一:
msoBarNoProtection—不受保护,可自定义(缺省值);
msoBarNoCustomize—不能自定义;
msoBarNoResize—不能调整大小;
msoBarNoMove—不能移动;
msoBarNoChangeVisible—不能更改可见状态;
msoBarNoChangeDock—不能改变停靠的位置;
msoBarNoVerticalDock—不能沿窗口左侧或右侧停放;
msoBarNoHorizontalDock—不能沿窗口顶部或底部停放。
(7)Top属性,以像素为单位返回命令栏控件顶边相对于屏幕顶边的距离。
(8)Type属性,返回代表命令栏类型的常数。可以为下列值之一:
msoBarTypeNormal—工具栏;
msoBarTypeMenuBar—菜单栏;
msoBarTypePopUp—快捷菜单。
(9)Visible属性,返回命令栏是否可见。
5 命令栏中的控件
CommandBar对象包含CommandBarControl对象,其Controls属性可以访问CommandBarControl对象。有3种类型的控件,即按钮控件(一个CommandBarButton对象)、组合框控件(一个CommandBarComboBox对象)和菜单(一个CommandBarPopup对象)。
下面的过程列出命令栏中的所有控件:
Dim row As Integer
Dim cBar As CommandBar
Dim ctl As CommandBarControl
Dim Menu As CommandBarControl
Dim MenuItem As CommandBarControl
Dim SubMenuItem As CommandBarControl
Cells.Clear
'设置标题
Cells(1, 1) = "索引值"
Cells(1, 2) = "命令栏名称"
Cells(1, 3) = "子项名称"
Range(Cells(1, 1), Cells(1, 3)).Font.Bold = True
Cells(2, 1) = "工具栏"
Cells(2, 1).Font.Italic = True
row = 3
'列出工具栏控件
For Each cBar In CommandBars
If cBar.Type = msoBarTypeNormal Then
Cells(row, 1) = cBar.Index
Cells(row, 2) = cBar.Name
For Each ctl In cBar.Controls
Cells(row, 3) = ctl.Caption
row = row + 1
Next ctl
End If
Next cBar
'列出菜单
Cells(row, 1) = "菜单栏"
Cells(row, 1).Font.Italic = True
row = row + 1
For Each cBar In CommandBars
If cBar.Type = msoBarTypeMenuBar Then
Cells(row, 1) = cBar.Index
Cells(row, 2) = cBar.Name
For Each ctl In cBar.Controls
Cells(row, 3) = ctl.Caption
row = row + 1
Next ctl
End If
Next cBar
'列出快捷菜单
Cells(row, 1) = "快捷菜单"
Cells(row, 1).Font.Italic = True
row = row + 1
For Each cBar In CommandBars
If cBar.Type = msoBarTypePopup Then
Cells(row, 1) = cBar.Index
Cells(row, 2) = cBar.Name
For Each ctl In cBar.Controls
Cells(row, 3) = ctl.Caption
row = row + 1
Next ctl
End If
Next cBar
'设置标题
row = row + 1
Cells(row, 1) = "菜单名称"
Cells(row, 2) = "菜单命令"
Cells(row, 3) = "子菜单"
Range(Cells(row, 1), Cells(row, 3)).Font.Bold = True
row = row + 1
'列出菜单信息
On Error Resume Next
For Each Menu In CommandBars(1).Controls
For Each MenuItem In Menu.Controls
For Each SubMenuItem In MenuItem.Controls
Cells(row, 1) = Menu.Caption
Cells(row, 2) = MenuItem.Index & MenuItem.Caption
Cells(row, 3) = SubMenuItem.Index & SubMenuItem.Caption
row = row + 1
Next SubMenuItem
Next MenuItem
Next Menu
'工作表设置
Cells.Columns.AutoFit
Rows("1:1").RowHeight = 29.25
ActiveWindow.FreezePanes = True
End Sub
示例工作簿下载地址:
下载命令栏控件清单
6 命令栏控件的属性
下面列出命令栏控件的一些常用属性及说明:
(1)BeginGroup属性,设置为True时会在控件前显示一个分隔条;
(2)BuiltIn属性,若控件为内置控件,则该属性的值为True;
(3)Caption属性,为控件上显示的文本;
(4)Enabled属性,设置为True时可以单击该控件;
(5)FaceID属性,代表图形图像的数字;
(6)Id属性,代表内置控件的代码编号;
(7)OnAction属性,指定单击控件时执行的VBA过程名称;
(8)State属性,确定控件是否呈现按下状态,仅用于CommandBarButton控件;
(9)Style属性,确定控件显示时是否包含标题或图像,只能用于CommandBarButton控件和CommandBarComboBox控件;
(10)ToolTipText属性,控件的提示文本,当鼠标移动到控件上方时呈现;
(11)Type属性,表示控件类型。
下面的示例文档列出了Excel中所有菜单和工具栏的名称及相应的ID编号,可以在编程时参考。
示例文档下载地址:
Excel菜单和工具栏名称及ID

发表评论