1

我有一个带有一堆文本框的用户表单。最终用户将测试数据输入表格,点击“提交”按钮后,此信息将保存到 Excel 表中。我想知道如何使用户可以将计算机中的图像输入到表单中。

要求:最终用户能够从他们的计算机中选择图像,然后单击表单上的提交以将其插入到 Excel 工作表中。

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
4

1 回答 1

2

当然,这里有一个示例代码,可以让您对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
于 2013-08-04T18:15:49.417 回答