Insert an image into a userform using the browse option.

Solved
Bob0876 Posted messages 41 Status Member -  
Bob0876 Posted messages 41 Status Member -
Hello, I am creating a form and I would like to allow the display of any image using the browse option.

What I mean is that anyone who uses my form can add an image by searching for it in their folders.

Thank you,

3 answers

pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 772
 
Hello,

In your UserForm:
- draw an Image control (named Image1)
- draw a CommandButton (named CommandButton1, Caption property: Browse...)

The code for your button is:
Private Sub CommandButton1_Click() Dim FichImg As Variant FichImg = Application.GetOpenFilename("All file types (*.*),*.*") 'If no file is selected, go to error handling (NoFile) If FichImg = False Then GoTo NoFile 'If the selected file is not an image 'the error handling will process the insertion line 'do nothing and go to error handling (NotAnImage) On Error GoTo NotAnImage 'Attempt to insert the file in the Image1 control Me.Image1.Picture = LoadPicture(FichImg) Exit Sub 'error handling NoFile: MsgBox "You must select an image file to insert.", vbCritical Exit Sub NotAnImage: MsgBox "The chosen file format is not suitable.", vbCritical End Sub


--
🎼 Best regards,
Franck 🎶
3
Bob0876 Posted messages 41 Status Member
 
Thank you very much, but I took your code, I created the two subs (pasuneimage and pasdefichier) in a module, however when I click the button an error occurs and it says label not defined? Why?
0
Bob0876 Posted messages 41 Status Member
 
Lol It's my fault, I had entered your code incorrectly!! Thank you very much, it's working perfectly.
0