can add icons as buttons without add commandbutton on userform

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
430
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm not sure what I ask maybe could in excel.
as known when add icon into commandbutton on form will not show the caption clearly after upload icon . I have to resize the button then will be much big to show caption. my goal I will design the button by PHOTHOSHOP and I will upload to excel.
my question is: can I add the icon as picture on form without add commandbutton like inside sheet will use some shapes (rectangular, oval) and link it with macro?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You could add an image control and use that, but you can't add shapes.
 
Upvote 0
You could add an image control and use that
but how add png image to commandbutton
I have the code about png but shows error when link with commandbutton1
like this
VBA Code:
Private Sub UserForm_Activate()
Dim vntFilename As Variant
    
    vntFilename = Application.GetOpenFilename("Images (*.png),*.png")
    If vntFilename = "False" Then Exit Sub
    
    CommandButton1.Caption = LoadImage(vntFilename)
End Sub
 
Upvote 0
use the picture property not caption
 
Upvote 0
unfortunately doesn't show picture and doesn't show error!
VBA Code:
Private Sub UserForm_Activate()
Dim vntFilename As Variant
    
    vntFilename = Application.GetOpenFilename("Images (*.png),*.png")
    If vntFilename = "False" Then Exit Sub
    
    CommandButton1.Picture = LoadImage("C:\Users\MK\Pictures\qw.png")
End Sub
 
Upvote 0
I see label could use it so I suppose commandbutton should use it too.
but doesn't work
VBA Code:
Private Sub UserForm_Activate()
Dim vntFilename As Variant
    
    vntFilename = Application.GetOpenFilename("Images (*.png),*.png")
    If vntFilename = "False" Then Exit Sub
    
    Me.CommandButton1.Picture = LoadImage("C:\Users\MK\Pictures\qw.png,Me.CommandButton1")
End Sub
 
Upvote 0
Again, you cannot use a png format like that.
 
Upvote 0
Hi,
I'm not sure what I ask maybe could in excel.
as known when add icon into commandbutton on form will not show the caption clearly after upload icon . I have to resize the button then will be much big to show caption. my goal I will design the button by PHOTHOSHOP and I will upload to excel.
my question is: can I add the icon as picture on form without add commandbutton like inside sheet will use some shapes (rectangular, oval) and link it with macro?
As @RoryA said, you need to use the Picture property (and not caption). And as VBA does not natively handle PNG, you will need to add the either the function referred above, or this one (which is simpler and easier to use, in my opinion):

VBA Code:
Function LoadImage(ByVal Filename As String) As StdPicture
  With CreateObject("WIA.ImageFile")
    .LoadFile Filename
    Set LoadImage = .FileData.Picture
  End With
End Function

So you final code would look something like:


VBA Code:
Private Sub UserForm_Activate()
  Dim vntFilename As Variant 
  vntFilename = Application.GetOpenFilename("Images (*.png),*.png")
  If vntFilename = "False" Then Exit Sub
  Me.CommandButton1.Picture = LoadImage(vntFilename)
End Sub

Function LoadImage(ByVal Filename As String) As StdPicture
  With CreateObject("WIA.ImageFile")
    .LoadFile Filename
    Set LoadImage = .FileData.Picture
  End With
End Function

More about dealing with PNG files here: Working with images in VBA - Displaying PNG files
 
Upvote 0
Thanks Dan ,

I see this is not useful way ,because it doesn't save image after close form when load image to commandbutton1 . I have to load image every time run the form . this is disappointed, sorry !
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,743
Members
453,370
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top