Question

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:

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:

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.

Was it helpful?

Solution

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:

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)

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)

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.)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top