vba 在VBA中将图像插入用户表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21606413/
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
Inserting image into userform in VBA
提问by Hung Luong
I am creating an vba userform in Word. Now I'm working on a feature that let the user browse for an image file, load it onto an Image control in the userform and display the filepath in a textbox (sort of a image preview feature that also allow user to directly change file by editing the textbox). With help I have figured out the code needed for a single "Browse" button as follow:
我正在 Word 中创建一个 vba 用户表单。现在我正在开发一个功能,让用户浏览图像文件,将其加载到用户窗体中的图像控件上并在文本框中显示文件路径(某种图像预览功能,也允许用户通过以下方式直接更改文件)编辑文本框)。在帮助下,我找到了单个“浏览”按钮所需的代码,如下所示:
Private Sub btnBrowse1_Click()
Dim fd As FileDialog
Dim strPicPath1 As String
Dim vrtSelected As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
For Each vrtSelected In .SelectedItems
AutoReport.txtboxPicPath1.Text = vrtSelected
strPicPath1 = vrtSelected
AutoReport.Image1.Picture = LoadPicture(strPicPath1)
Next vrtSelected
Else: Exit Sub
End If
End With
End Sub
However as I have around 8 of those Browse buttons, I want to "generalize" the process by writing a procedure so that I can just call it instead of writing the same structure 8 times, something like this:
然而,由于我有大约 8 个浏览按钮,我想通过编写一个过程来“概括”该过程,以便我可以调用它而不是编写相同的结构 8 次,如下所示:
Sub FormLoadPicture(TxtboxToFill As TextBox, ImageBox As Image, objForm As Object)
Dim fd As FileDialog
Dim vrtSelected As Variant
Dim PicPath As String
Dim i As Long
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
For Each vrtSelected In .SelectedItems
objForm.TxtboxToFill.Text = vrtSelected
PicPath = vrtSelected
objForm.ImageBox.Picture = LoadPicture(PicPath)
Next vrtSelected
Else: Exit Sub
End If
End With
End If
Next i
End Sub
When I call it (i.e FormLoadPicture(txtboxPicPath2,Image2,me), it gave me a "Syntax error". I am suspecting that I am writing the code for referencing the form control wrong but I have no idea what should be right.
当我调用它时(即 FormLoadPicture(txtboxPicPath2,Image2,me),它给了我一个“语法错误”。我怀疑我正在编写引用表单控件的代码错误,但我不知道什么应该是正确的。
采纳答案by KekuSemau
Welcome to this forum.
I see several points here:
欢迎来到本论坛。
我在这里看到几点:
1.)
Yes, this is confusing, but types like Textbox
and Image
seem to refer to the ActiveX-controls-types which are used on a worksheet. The controls on userforms have different types, and you have to use this here:
1.) 是的,这是令人困惑的,但是类型像Textbox
并且Image
似乎是指在工作表上使用的 ActiveX 控件类型。用户窗体上的控件有不同的类型,你必须在这里使用它:
Sub FormLoadPicture(TxtboxToFill As MSForms.TextBox, _
ImageBox As MSForms.Image, objForm As MSForms.UserForm)
2.)
If you don't use AllowMultiSelect
, you don't have to use a loop, just use .SelectedItems(1)
2.)
如果不使用AllowMultiSelect
,则不必使用循环,只需使用.SelectedItems(1)
3.)
Your parameters are references to objects, you can (only) use them as such, not as properties of the userform.
That means ImageBox.Picture = LoadPicture(PicPath)
instead of objForm.ImageBox.Picture = LoadPicture(PicPath)
3.)
您的参数是对对象的引用,您可以(仅)将它们用作对象,而不是用作用户表单的属性。
这意味着ImageBox.Picture = LoadPicture(PicPath)
而不是objForm.ImageBox.Picture = LoadPicture(PicPath)
4.)
When you call the sub, you may not use brackets, as in FormLoadPicture(txtboxPicPath2,Image2,me)
This works (and is often seen) if there is only one argument, but has a different meaning (putting parameters in brackets enforces passing them by value).
(Only function calls on the right side of an assignment are put into brackets in VBA.)
4.) 当您调用 sub 时,您可能不会使用括号,如FormLoadPicture(txtboxPicPath2,Image2,me)
This 有效(并且经常看到),如果只有一个参数,但具有不同的含义(将参数放在括号中强制按值传递它们)。
(在 VBA 中,仅将赋值右侧的函数调用放入括号中。)
Private Sub CommandButton1_Click()
FormLoadPicture Me.TextBox1, Me.Image1, Me
End Sub
Sub FormLoadPicture(TxtboxToFill As MSForms.TextBox, _
ImageBox As MSForms.Image, objForm As MSForms.UserForm)
Dim fd As FileDialog
Dim PicPath As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
If .Show = -1 Then
PicPath = .SelectedItems(1)
TxtboxToFill.Text = PicPath
ImageBox.Picture = LoadPicture(PicPath)
Else: Exit Sub
End If
End With
End Sub