VBA - Resize Image In a CommandButton (Excel)
Posted on 2010-09-08
I would like to resize an image in a button without stretching the image. I would like the image to fit in the button as large as possible without appearing distorted. In addition, I would like to attach a macro to the button click event. Thanks in advance for your help.
Sub CreateBtn(StartCell As Range, EndCell As Range)
Dim Obj As Object
Dim Code As String
'Set button coordinates with cells
t = .Top
l = .Left
w = EndCell.Offset(0, 1).Left - .Left
h = EndCell.Offset(1, 0).Top - .Top
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=l, Top:=t, Width:=w, Height:=h)
Obj.Name = "TestButton"
Obj.Object.Picture = LoadPicture(ActiveWorkbook.Path & "\example.bmp")
'parameters desired height / width don't work???
'Need to shink but not stretch
ActiveSheet.OLEObjects(1).Object.Caption = "Test Button"