Excel VBA:最终用户在计算机上选择图像并在用户表单上提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18029222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA: end user select image on computer and submit on user form
提问by Erica
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.
我有一个带有一堆文本框的用户表单。最终用户将测试数据输入表单,单击“提交”按钮后,此信息将保存到 Excel 表中。我想知道如何让用户将图像从他们的计算机输入到表单中。
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.
要求:最终用户能够从他们的计算机中选择一个图像,然后单击表单上的提交以将其插入到 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
采纳答案by KekuSemau
Sure, here is a sample code that may give you an idea about FileDialog
.
当然,这里有一个示例代码,可以让您了解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