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