Change the image of a toolbar button

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Change the image of a toolbar button

Post by Robie »

Hi

Hopefully, this is simple.

I am creating a toolbar using PPA macro under PP2003/2007. How can I add a specific image to the toolbar button? I am using the following code. I originally set it up to be a pig (FaceId 52) but don't know how to change that so that I can replace it with my own specific image/icon.

Thanks.

Code: Select all

Sub Auto_Open()
    Dim oToolbar As CommandBar
    Dim oButton As CommandBarButton
    Dim MyToolbar As String

    ' Give the toolbar a name
    MyToolbar = "ChangeColour"

    On Error Resume Next
    ' so that it doesn't stop on the next line if the toolbar's already there

    ' Create the toolbar; PowerPoint will error if it already exists
    Set oToolbar = CommandBars.Add(Name:=MyToolbar, Position:=msoBarTop, Temporary:=False)
    If Err.Number <> 0 Then
        ' The toolbar's already there, so we have nothing to do
        Exit Sub
    End If

    On Error GoTo ErrorHandler

    ' Now add a button to the new toolbar
    Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
    ' And/set some of the button's properties
    With oButton
         .DescriptionText = "Change colour of the presentation" ' Tooltip text when mouse if placed over button
         .Caption = "Change Colour"                             ' Text if Text in Icon is chosen
         .OnAction = "ShowColourSelection"                      ' Runs ShowColourSelection macro when clicked
         .Style = msoButtonIcon                                 ' Button displays as icon, not text or both
         .FaceId = 52                                                 ' 52 is my favorite pig until Simon changes it
    End With

    ' You can set the toolbar position and visibility here if you like
    ' By default, it'll be visible when created
    oToolbar.Top = 150
    oToolbar.Left = 150
    oToolbar.Visible = True
NormalExit:
    Exit Sub   ' so it doesn't go on to run the errorhandler code
ErrorHandler:
     'Just in case there is an error
     MsgBox Err.Number & vbCrLf & Err.Description
     Resume NormalExit:
End Sub

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change the image of a toolbar button

Post by HansV »

Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Change the image of a toolbar button

Post by Robie »

Thank you Hans - sorry for late reply was having all sorts of nightmare with my PC not being backed up and losing lots of stuff.

In the end, I went with this option. I created two toolbars, one hidden with the 'required' image as the button. The other toolbar would be created with the .ppa file and then within Auto_Open, it would copy the 'required' image over the displayed toolbar.

User avatar
HansV
Administrator
Posts: 78237
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Change the image of a toolbar button

Post by HansV »

That has the added advantage that you don't have to rely on external image files.
Best wishes,
Hans