Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Button Images Set at Run Time 1

Status
Not open for further replies.

caffrinho

MIS
Mar 1, 2002
91
GB
Please help, this is driving me nuts!!

I want to add custom images to a toolbar button. Easy if you do it manually, but i want my add-in to install buttons with custom images during install.

I've pinched the following code from VB Help in the editor (IIRC). However, i can't get it to run. It errors on the line where it's locating the image (set oShape=...), with the following message.

"runtime error '424' - Object required"

My first reaction was that the location/image name was wrong. But it isn't.

I've also made sure that the images are compatible. 16x16 pixels. In fact i created them in excel and exported them. I've tried making them bitmaps/gif's/jpegs with no luck.

I'm hoping i've missed something obvious, that somebody will point out to me. Or show me another way to achieve this.

thanks

Code:
Sub AddPictureToButton()
Dim oShape As Shape
Dim customBar As CommandBar
    
    
    'Place the graphic in the document
    Set oShape = ActiveDocument.Shapes.AddPicture("c:\buttons\test.gif") 
   
    Set customBar = Application.CommandBars("Toolbar")
    
    oShape.Select

    'Copy graphic
    Selection.CopyAsPicture

    'Delete from document
    Selection.Delete

    'Copy the graphic to the first button on the toolbar
    customBar.Controls(1).PasteFace

    Set oShape = Nothing

End Sub
 
Excel is different from word. You need proper reference to sheet, after this you will get missing arguments in AddPicture:
Set oShape = ActiveSheet.Shapes.AddPicture("c:\buttons\test.gif", True, True, 0, 0, 10, 10)
Set customBar = Application.CommandBars("Toolbar")
oShape.CopyPicture
customBar.Controls(1).PasteFace

You can try to directly load picture, however this loses transparency:
Application.CommandBars("Toolbar").Controls(1).Picture = LoadPicture("c:\buttons\test.gif")

combo
 
excellent... just what i needed.

Didn't realise the code i'd used was for word, tbh!

thanks, and have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top