How to use VBA to set a custom graphic on a toolbar button

Article contributed by Astrid Zeelenberg

You might find that you are happy to use one of the 1,707 button images that are built into Word. You can get a complete list of the images and their FaceIDs from the download file in following article: Assigning custom button images to your toolbar and menu buttons. Once you've chosen a FaceID you want to use, you can assign it to a button at runtime using:

CommandBars("MyTemplateToolbar").Controls(1).FaceID = 'the number you chose

But if you want to create your own button images, read on. 

Method 1: Pasting the image using an external graphic

Sub AddPictureToButton()

Dim oShape As Shape

    'Place the graphic in the document
    Set oShape = ActiveDocument.Shapes.AddPicture(FileName:= _
             "PathAndNameOfPicture.gif")

    oShape.Select

    'Copy graphic
    Selection.CopyAsPicture

    'Delete from document
    Selection.Delete

    'Copy the graphic to the first button on your toolbar
    'Replace the name of the commandbar with your own
 
  CommandBars("TemplateCommandbar").Controls(1).PasteFace

    Set oShape = Nothing

End Sub

Your image canvas size must be 16 x 16 pixels, and the background colour must be RGB 191, 191, 191. The Gif format seems to work most reliably for this, in most cases, and it helps if you make the background colour transparent.

See also Assigning custom button images to your toolbar and menu buttons.
  

Method 2: Using a dummy disabled toolbar

There are several reasons why some people may not want to use the above method:
  

You may not want to have to distribute the Gif file with the template.

You may not want to have to rely on a document being open when you run the code.

If you want to be able to disable the button in question (for instance, when there are no documents open); and if you want the disabled button image to look as it should, then you may find that you have to modify the button manually after pasting it. (This is particularly an issue if you or any of your users are using Word 97). See also Assigning custom button images to your toolbar and menu buttons for a more detailed discussion of this issue.

To get round this:
  

1.

Create a "dummy" toolbar in your template at design time. Using VBA, add as many buttons to it as are needed to store all the button images that you want to be able to assign programmatically, but without giving the buttons any OnAction property, as it's simply going to be an image container. Give each button a caption, so that they are easy to identify later, e.g.:

With CommandBars("Dummy").Controls.Add
    .Caption = "Button1"
End With
  

2.

Then add your button images manually to the dummy controls, and if appropriate, select Tools + Customize, right-click the button and select Edit Button Image”, click where it says Erase, and erase all of the background grey, so that it will display correctly if you ever need to disable the button.
  

3.

Disable the dummy toolbar using:

CommandBars("Dummy").Enabled = False 1
  

4.

At runtime you can then use code like the following:

CommandBars("Dummy").Controls("Button1").CopyFace
CommandBars("TemplateCommandbar").Controls(1).PasteFace
ThisDocument.Saved = True

(You need to set the template's Saved property to True, so that the user won't subsequently be asked if they want to save changes to it.)

__________________

1.

Note that the Enabled property of a toolbar is not stored in the template that the toolbar is stored in, even though it's a property of that toolbar! Instead, it is stored in the Data Key; so it's also a good idea to disable it in the template's AutoOpen macro (if it's a document template) or its AutoExec macro (if it's a Global template), just in case the user's Data Key becomes corrupted.

By contrast, the Enabled property of a toolbar button (CommandBarControl) is stored in the template that the toolbar is stored in; so if you disable a button, you need to immediately mark the template as Saved”:, using:

ThisDocument.Saved = True


Click to view Terms of Use page

Click to view Disclaimer page