Forum Discussion

vjjmm's avatar
vjjmm
Copper Contributor
Feb 07, 2022
Solved

VBA: how do I enter an image into a userform?

Hi guys, I am doing a userform in which I need the users to select an image from their desktop and upload it in the userform. does anyone have an Idea of how to do that? the userform info+the pictures need to be then saved in a database which I made in a second sheet.

  • vjjmm 

    Create an Image control on the userform and set its PictureSizeMode property to 3 - fmPictureSizeModeZoom.

    Create a command button on the userform, name it cmdPicture and create the following On Click event procedure for it:

    Private Sub cmdPicture_Click()
        Dim strPic As String
        With Application.FileDialog(1) ' msoFileDialogOpen
            .Filters.Clear
            .Filters.Add "Image Files (*.jpg, *.png, *.bmp, *.gif)", "*.jpg,*.png,*.bmp,*.gif"
            If .Show Then
                strPic = .SelectedItems(1)
                Me.Image1.Picture = LoadPicture(strPic)
            Else
                Beep
            End If
        End With
    End Sub

    Here, Image1 is the name of the Image control.

    See the attached sample workbook.

2 Replies

  • vjjmm 

    Create an Image control on the userform and set its PictureSizeMode property to 3 - fmPictureSizeModeZoom.

    Create a command button on the userform, name it cmdPicture and create the following On Click event procedure for it:

    Private Sub cmdPicture_Click()
        Dim strPic As String
        With Application.FileDialog(1) ' msoFileDialogOpen
            .Filters.Clear
            .Filters.Add "Image Files (*.jpg, *.png, *.bmp, *.gif)", "*.jpg,*.png,*.bmp,*.gif"
            If .Show Then
                strPic = .SelectedItems(1)
                Me.Image1.Picture = LoadPicture(strPic)
            Else
                Beep
            End If
        End With
    End Sub

    Here, Image1 is the name of the Image control.

    See the attached sample workbook.

Resources