简体   繁体   中英

Excel VBA: end user select image on computer and submit on user form

I have a userform with a bunch of textboxes. End user inputs test data into form and after clicking "Submit" button, this info is saved to an excel sheet. I am wondering how to make it possible for user to input images from their computer into the form.

Requirements: End user be able to select an image from their computer and click submit on the form to have it inserted into an excel sheet.

Private Sub CommandButton3_Click()
    Dim image As FileDialog 
    Set image = Application.FileDialog(msoFileDialogFilePicker) 
    Dim vrtSelectedPicture As Variant 
    With image 
        If .Show = -1 Then
        For Each vrtSelectedPicture In .SelectedItems                 
            'Show path in textbox
            TextBox71.Text = .SelectedItems(1)      
        Next vrtSelectedPicture
        'The user pressed Cancel.
        Else
        End If
    End With
    'Set the object variable to Nothing
    Set image = Nothing
End Sub

Sure, here is a sample code that may give you an idea about FileDialog .

Private Sub CommandButton1_Click()

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Select an image file"
        .Filters.Add "Image", "*.gif; *.jpg; *.jpeg", 1
        If .Show = -1 Then
            ' file has been selected

            ' e.g. show path in textbox
            Me.TextBox1.Text = .SelectedItems(1)

            ' e.g. display preview image in an image control
            Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
            Me.Image1.Picture = LoadPicture(.SelectedItems(1))
        Else
            ' user aborted the dialog

        End If
    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM