locked
how to embed a picture in a command button on a excel User Form ? RRS feed

  • Question

  • hi to all experts,

    i have a simple question 

    i like to insert picture to background of my commandbuttons  on Userform.

    pls let me know your great idea in this respect.

    Rgrds

    Nima

    Sunday, July 21, 2013 10:37 AM

Answers

  • My apologies, I just typed the code without testing it. Change the line

    Private Sub cmdTest_Exit()

    to

    Private Sub cmdTest_Exit(ByVal Cancel As MSForms.ReturnBoolean)


    Regards, Hans Vogelaar

    • Marked as answer by Nima Iranian Tuesday, July 23, 2013 8:16 AM
    Sunday, July 21, 2013 2:05 PM
  • Is there enough room on the command button to display the image and caption? If the image is large, it may cover the caption.

    Regards, Hans Vogelaar

    • Marked as answer by Nima Iranian Monday, July 22, 2013 10:28 AM
    Monday, July 22, 2013 6:06 AM

All replies

  • Interactively:

    • Select the command button on the userform in the Visual Basic Editor.
    • Click in the Picture property, then click the builder dots ... on the right hand side.
    • Select a picture, then click OK.
    • Click in the PicturePosition property, then select a position from the dropdown list (it's best to experiment).

    Using code, you can use the LoadPicture function. For example:

        With Me.cmdTest
            .Picture = LoadPicture("C:\Images\Test.jpg")
            .PicturePosition = fmPicturePositionAboveCenter
        End With


    Regards, Hans Vogelaar

    • Proposed as answer by Jeffrey_Chen_ Tuesday, July 23, 2013 7:21 AM
    Sunday, July 21, 2013 10:57 AM
  • Dear Hans Vogelaar,

    Many thanks for your prompt reply.

    now i need to change the picture while clicking. i mean when i click on the button bg picture has to changed

    to show button clicked.

    pls advise your idea.

    regards

    Nima 

    Sunday, July 21, 2013 11:23 AM
  • Loading an image from disk is probably not an attractive idea if you want to change the image frequently.

    As an alternative, place an image control Image1 on your userform.

    Set its Picture property as described above, and set its Visible property to False.

    Set the Picture of the command button to the Picture of the image control in the On Click event procedure of the command button:

    Private Sub cmdTest_Click()
        Me.cmdTest.Picture = Me.Image1.Picture
    End Sub

    where cmdTest is the name of the command button and Image1 is the name of the image control.

    Regards, Hans Vogelaar

    Sunday, July 21, 2013 11:40 AM
  • Dear Hans Vogelaar,

    that was a nice Idea.

    i have inserted 2 Image control to the user form .

    image 1 set to the primary(default) image of cmdTest and image 2 as your above post set to clicked cmdTest.

     

    but when i clicking on other buttons i wanna cmdTest  BG Picture turned to image 1 to show cmdTest isn't clicked right now.

    hope i could describe clearly what i want exactly.

    regards

    Nima

    Sunday, July 21, 2013 1:09 PM
  • You can add an event procedure for the On Exit event of the command button:

    Private Sub cmdTest_Click()
        Me.cmdTest.Picture = Me.Image2.Picture
    End Sub
    
    Private Sub cmdTest_Exit()
        Me.cmdTest.Picture = Me.Image1.Picture
    End Sub


    Regards, Hans Vogelaar

    Sunday, July 21, 2013 1:21 PM
  • http://social.msdn.microsoft.com/Forums/getfile/310628

    i got above linked image Error !!!

    Sunday, July 21, 2013 1:55 PM
  • My apologies, I just typed the code without testing it. Change the line

    Private Sub cmdTest_Exit()

    to

    Private Sub cmdTest_Exit(ByVal Cancel As MSForms.ReturnBoolean)


    Regards, Hans Vogelaar

    • Marked as answer by Nima Iranian Tuesday, July 23, 2013 8:16 AM
    Sunday, July 21, 2013 2:05 PM
  • Dear Hans Vogelaar,

    your above codes work greatly.

    just a simple question:

    if i wanna my All Command buttons in the user form have no image by default and just when they are clicking, their BG picture change, is there any code to do it automatically, i mean if any solution to prevent writing above two codes for each command button.  

    regards

    Nima

    • Marked as answer by Nima Iranian Monday, July 22, 2013 5:35 AM
    • Unmarked as answer by Nima Iranian Monday, July 22, 2013 5:35 AM
    Monday, July 22, 2013 4:24 AM
  • It would be possible to create a class for this, but I don't think it'd be less work. I'd create the two event procedures for each command button.

    Regards, Hans Vogelaar

    Monday, July 22, 2013 5:08 AM
  • Dear Hans Vogelaar,

    thanks for your efforts, just one more question:

    when i click on the Command button and BG change, why caption isn't visible?

    is there any solution to sort it out?

    regatds

    Nima

    Monday, July 22, 2013 5:39 AM
  • Is there enough room on the command button to display the image and caption? If the image is large, it may cover the caption.

    Regards, Hans Vogelaar

    • Marked as answer by Nima Iranian Monday, July 22, 2013 10:28 AM
    Monday, July 22, 2013 6:06 AM
  • Dear Hans,

    a lots of thanks for your good guidance.

    i adjust the picture and caption is visible

    many thanks for your efforts

    B.Regards

    Nima Iranian

    Monday, July 22, 2013 10:28 AM
  • I use dialoguebox and take the path of the image and set the commandbutton Picture but It happens 1 time when I try to change the picture of same commandButton it is not change why this is happen
    thank you 
    regards 
    ahwmnm 


    Friday, July 8, 2016 10:29 PM
  • If I use code like the following, the picture is changed each time I select a different one:

    Private Sub CommandButton1_Click()
        Dim strFile As String
        strFile = Application.GetOpenFilename("Picture files,*.jpg")
        If strFile <> "False" Then
            Me.CommandButton1.Picture = LoadPicture(strFile)
        End If
    End Sub

    What code are you using?


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, July 8, 2016 11:55 PM