Working with Images on Command Bar ButtonsThis content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Every built-in command bar button has an image associated with it. You can use these images on your own command bar buttons as long as you know the FaceId property value of the built-in button that contains the image. The values for the FaceId property range from zero (no image) to the total number of button images used in the host application (typically a few thousand). One easy way to browse the available button images is to build a toolbar, add some buttons, and assign FaceId property values to those buttons. The buttons display the image associated with the specified FaceId property value. For example, to see button images with values from 200 to 299, you would call CBShowButtonFaceIDs from the Immediate window in this way:
? CBShowButtonFaceIDs(200, 299)
And the button images would display as shown in the following figure.
A Collection of Built-in Toolbar Icons
You can see the value of the FaceId property for any image on the command bar by resting your mouse pointer on the image until the value appears in the button's ToolTip.
Another way to copy the image from one command bar button to another is to use the FindControl method of the CommandBars collection to determine the value of the FaceId property for the image you want to copy. Then, you can use the CommandBarControl object's CopyFace and PasteFace methods to copy the image to a new control. The following sample code illustrates how to use these methods to paste the icon associated with an existing command bar button to a new command bar button.
Private Sub CBCopyIconDemo() ' This procedure demonstrates how to copy the image associated ' with a known toolbar button to a new toolbar button. This example ' copies the image associated with the "Contents and Index" control ' on the Help menu to a new command bar control. Dim cbrNew As CommandBar Dim ctlNew As CommandBarControl Const ERR_CMDBAR_EXISTS As Long = 5 On Error Resume Next Set cbrNew = CommandBars.Add("TestCopyFaceIcon") If Err = ERR_CMDBAR_EXISTS Then Call CBDeleteCommandBar("TestCopyFaceIcon") Set cbrNew = CommandBars.Add("TestCopyFaceIcon") ElseIf Err <> 0 Then Exit Sub End If On Error GoTo 0 Set ctlNew = cbrNew.Controls.Add(msoControlButton) Call CBCopyControlFace("Help", "Contents and Index") With ctlNew .PasteFace End With cbrNew.Visible = True End Sub
This procedure calls two other custom procedures. The CBDeleteCommandbar procedure was discussed in the previous section and is used to delete the command bar if it already exists. The CBCopyControlFace procedure copies the image of the specified control to the Clipboard:
Function CBCopyControlFace(strCBarName As String, _ strCtlCaption As String) ' This procedure uses the CopyFace method to copy the image associated ' with the control specified in the strCtlCaption argument to the Clipboard. Dim ctlCBarControl As CommandBarControl Set ctlCBarControl = CommandBars.FindControl(msoControlButton, _ CBGetControlID(strCBarName, strCtlCaption)) ctlCBarControl.CopyFace End Function
The CBCopyControlFace procedure uses the CBGetControlID procedure as the Id argument for the FindControl method. CBGetControlID returns the Id property for the specified control by using the following line:
CBGetControlID = Application.CommandBars(strCBarName) _ .Controls(strControlCaption).ID
The CommandBar object also supports a FindControl method that searches for the specified control only on the CommandBar object itself.
To see these procedures working together, place the insertion point (cursor) anywhere in the CBCopyIconDemo procedure and use the F8 key to step through the code. Try changing the command bar name and control name to copy different images to the new control.
Working with Command Bars | Manipulating Command Bars and Command Bar Controls with VBA Code | Getting Information About Command Bars and Controls | Creating a Command Bar | Hiding and Showing a Command Bar | Copying a Command Bar | Deleting a Command Bar | Preventing Users from Modifying Custom Command Bars | Working with Personalized Menus | Working with Command Bar Controls | Working with Command Bar Events