原文地址:
http://spreadsheetpage.com/index.php/site/tip/identifying_commandbar_images
从 Excel 97 到 Excel 2003 都在内置的菜单和工具栏中使用了大量的图标。你可以通过设置 FaceID 属性为特定的整数把这些内置的图标用在你的自定义菜单和工具栏上。但问题是如何确定每个图标对应的整数。
下面的子程序创建了一个包含前250个 FaceID 图标的自定义工具栏(见下图)。你可以通过修改 IDStart 和 IDStop 的值来看更多的图标。最后一个 FaceID 应该是3518(也包括很多的空图标)。
在创建工具栏后,将鼠标光标移动到一个按钮上可以查出图标的 FaceID 值。点击工具栏的按钮不会有任何反应因为子程序没有指定任何宏到 OnAction 属性。
显示 FaceId 的子程序
Sub ShowFaceIDs()
Dim NewToolbar As CommandBar
Dim NewButton As CommandBarButton
Dim i As Integer, IDStart As Integer, IDStop As Integer
' Delete existing FaceIds toolbar if it exists
On Error Resume Next
Application.CommandBars("FaceIds").Delete
On Error GoTo 0
' Add an empty toolbar
Set NewToolbar = Application.CommandBars.Add _
(Name:="FaceIds", temporary:=True)
NewToolbar.Visible = True
' Change the following values to see different FaceIDs
IDStart = 1
IDStop = 250
For i = IDStart To IDStop
Set NewButton = NewToolbar.Controls.Add _
(Type:=msoControlButton, Id:=2950)
NewButton.FaceId = i
NewButton.Caption = "FaceID = " & i
Next i
NewToolbar.Width = 600
End Sub
译注:上述代码仅适用于 Excel ,在 Excel 2003 中测试正常。如果需要在 Word 中运行,NewToolbar.Controls.Add 方法中的 Id 参数会导致报错,只需要去掉 Add 方法中的 Id 参数即可:
Sub ShowFaceIDs()
Dim NewToolbar As CommandBar
Dim NewButton As CommandBarButton
Dim i As Integer, IDStart As Integer, IDStop As Integer
' Delete existing FaceIds toolbar if it exists
On Error Resume Next
Application.CommandBars("FaceIds").Delete
On Error GoTo 0
' Add an empty toolbar
Set NewToolbar = Application.CommandBars.Add _
(Name:="FaceIds", temporary:=True)
NewToolbar.Visible = True
' Change the following values to see different FaceIDs
IDStart = 1
IDStop = 500
For i = IDStart To IDStop
Set NewButton = NewToolbar.Controls.Add _
(Type:=msoControlButton)
NewButton.FaceId = i
NewButton.Caption = "FaceID = " & i
Next i
NewToolbar.Width = 600
End Sub