Import a photo into a VBA userform

Solved
Meloooooo Posted messages 10 Status Membre -  
 Johann -
Hello,

As part of a software project, I need to create a VBA program. It is software for an equestrian center.
At one point in my project, I created a userform "InscriptionChevaux", allowing the user to enter a new horse into the software. The user will have to provide the name, date of birth, breed, etc. of the horse. However, I would also like the user to be able to upload the horse's photo.

For this, in my userform, I inserted an Image tool called "ImageCheval". I want a file explorer to appear when the user clicks on this tool so that the user can browse their PC to find the image.
To achieve this, I created the following macro within the "ImageCheval" tool:

Private Sub ImageCheval_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.Show If fd.SelectedItems.Count > 0 Then MsgBox fd.SelectedItems(1) End If Set fd = Nothing End Sub


This macro works perfectly; when the user clicks on "ImageCheval", the explorer appears and they can go to the photo anywhere on the PC. However, this macro does not import and save the chosen photo within my "ImageCheval" object. Indeed, I would like the photo to be saved in my Excel spreadsheet so that when the user wants to view the registered horses, the photo appears again.

Therefore, I would like to know how to import the photo into my userform as well as into my Excel spreadsheet...?

I hope I have been clear, and thank you in advance for your help :)

23 réponses

  • 1
  • 2
Meloooooo
 
Yes, I am indeed on Excel
The image is correctly in the Userform
And the code is in the image button of the Userform

So, here is the link: http://cjoint.com/?3KutMk9ZeFn

Once the document is open, click on "Enter", then "The Horses", then "Register a new horse" and finally click on the Image
1
lermite222 Posted messages 9042 Status Contributeur 1 199
 
In the references, add Ole Automation
With this, no more errors, but as mentioned earlier, the image does not appear
Add a button (instead of Click below to add a photo and put the code in this button.
A+

--
If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
Note: I do not respond to DMs for technical questions. And my crystal ball is broken.
1
lermite222 Posted messages 9042 Status Contributeur 1 199
 
To answer your second question as well as in my Excel spreadsheet...?
Your workbook is already heavy enough without adding all the photos.
I would rather suggest you ...
Create a subdirectory for your horse images
in the validate button to copy the selected image into this directory with a meaningful name like Clientxx.jpg
This way, when you need them, you just have to reload them.
See you later

--
If you bump into a pot and it sounds hollow, it’s not necessarily the pot that is empty. ;-)(Confucius)
Note: I do not respond to private messages for technical questions. And my crystal ball is broken.
1
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Hello,
importing an image is easy
In the Userform a PictureBox
 PictureBox1.Picture = LoadPicture(Path & imageName) 
in a sheet
Select the cell and..
 ActiveSheet.Pictures.Insert(Path & imageName).Select
However, with this, your images will be distorted; if you want to keep the ratio, it's much more complicated.
See this demo
Cheers
If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
Note: I don't respond to DMs for technical questions. And my crystal ball is broken.
0
Meloooooo Posted messages 10 Status Membre
 
Thank you very much for your responsiveness :)

I installed this element in my macro, but an error message appears saying "Compile error: Sub or Function not defined" and the debugger highlights "loadpicture"; do you know where this might come from?
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Where is the code located? In the UserForm module?
In VBA it's not PictureBox but Image
Image1.Picture = LoadPicture("C\Repertoire\Image1.Jpg") 

Show the exact and complete line where the error is located.
EDIT:
To center the image set the properties...
PictureAlignment to 2
PictureSizeMode to 3

If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
Note: I do not respond to DMs for technical questions. And my crystal ball is broken.
0
Meloooooo Posted messages 10 Status Membre
 
Thank you very much for your help :)

For Image and not PictureBox, thank you but I suspected it ;)

I can't insert a screenshot on the forum, but here is my macro (I have highlighted in bold the spot where the debugging puts me in the spotlight and when it gives me the alert message saying that Sub or Function is not defined):

Private Sub ImageCheval_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.Show If fd.SelectedItems.Count > 0 Then MsgBox fd.SelectedItems(1) ImageCheval.Picture = <gras>loadpicture</gras>(fd) End If Set fd = Nothing End Sub


This macro is located in the Userform and specifically in the Image.

However, I don't understand what you mean by:
"EDIT:
To center the image, set the properties..
PictureAlignment to 2
PictureSizeMode to 3 "
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
 ImageHorse.Picture = LoadPicture(fd.SelectedItems(1))

For the Edit:
In edit mode, right-click on the image control -> Properties
In the Properties window, you'll understand what I mean.
See you later

--
If you bump into a pot and it sounds hollow, it doesn't necessarily mean the pot is empty. ;-)(Confucius)
Note: I do not respond to private messages for technical questions. And my crystal ball is broken.
0
Meloooooo Posted messages 10 Status Membre
 
Indeed, I understand much better now about the properties ;) Thank you very much, I changed it as you told me!

I also changed the loadpicture according to your advice, but I still have the alert message from loadpicture saying "Compilation error: Sub or Function not defined."
I really don't understand...

Private Sub ImageCheval_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) fd.Show If fd.SelectedItems.Count > 0 Then MsgBox fd.SelectedItems(1) ImageCheval.Picture = <bold>LoadPicture</bold>(fd.SelectedItems(1)) End If Set fd = Nothing End Sub


Without wanting to take advantage of your kindness, do you have a new idea regarding the alert message?
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
In the editor, click on the menu..
Tools => References.
In the window, is there a reference marked "MISSING"?
If yes, tell me which one.

--
If you bump into a pot and it sounds hollow, it’s not necessarily the pot that’s empty. ;-)(Confucius)
Note: I do not respond to private messages for technical questions. And my crystal ball is broken.
0
Meloooooo
 
No, I don't see anything indicating "Missing"....
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
I notice that there is a problem with the image control, it doesn’t bug at my place but the image doesn’t want to show in the control.
Try putting the code in a button.

--
If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
Note: I do not respond to DMs for technical questions. And my crystal ball is broken.
0
Meloooooo
 
I just put my macro in a "CommandButton" and I'm having exactly the same problem as when the macro was in an "Image," which means the debugging gets stuck on loadpicure highlighting it...
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Are you sure about Excel?
Is the image on a UserForm?
Is the code in the UserForm module?
I don't see any other reason that could cause this problem.
I checked the help and tested several times...
Perhaps upload a copy of your workbook on Cjoint.com (without confidential data)
And put the link in a following post.
See you!
0
Meloooooo
 
You are AMAZING, it works!!!! :D

A big thank you to you and thank you for your great patience! :)
0
Meloooooo
 
Okay, thanks for the advice, I'll see about doing that.
And thank you for everything!
0
Meloooooo Posted messages 10 Status Membre
 
Good evening,
I had to put my VBA project aside for a bit, and it's now due very, very soon. Everything is almost finished, but I thought I could finish saving the photo on my own by saving the horse photos in a subdirectory when the user clicks "validate" (as you advised me), but I'm having a bit of trouble. I've been digging through books and the internet, but I can't find any topic on a forum that could correspond to mine. Lermite, could you help me again please by indicating how to proceed?
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
Re,
At the very top of the module, you put
 Dim MemoImage as String 

In the subroutine that allows you to put the image in the Image, just below..
 ImageCheval.Picture = LoadPicture(fd.SelectedItems(1)) 'You put MemoImage = fd.SelectedItems(1)

In the code of the Validate button
Dim Destination as string Destination = "C:\Directory\Image Directory\Client" & NumClient & ".jpg" FileCopy MemoImage, Destination

You say
If you bump into a pot and it sounds hollow, it's not necessarily the pot that's empty. ;-)(Confucius)
Note: I do not respond to PMs for technical questions. And my crystal ball is broken.
0
Meloooooo Posted messages 10 Status Membre
 
Good evening,
First of all, thank you very much for your responsiveness.
I put everything as you told me, no problem detected by the debugging. However, the photo does not save in the directory....
We agree that I need to replace "C:\Directory\Image Directory\Client" with the path specific to my computer and my directory?
0
lermite222 Posted messages 9042 Status Contributeur 1 199
 
How do you number the clients?
For testing..
 'After the line.. Destination = "C:\Directory\Image Directory\Client" & NumClient & ".jpg" 'You add MsgBox "Image = " & MemoImage & Chr(13) & "NumClient =" & NumClient _ & Chr(13) & "Destination = " & Destination

Put the result in a following post.
See you!
--
If you hit a pot and it sounds hollow, it’s not necessarily the pot that’s empty. ;-)(Confucius)
Note: I do not answer PMs for technical questions. And my crystal ball is broken.
0
Meloooooo Posted messages 10 Status Membre
 
Thank you very much for your response.
I found out where my error came from; I was missing a "\" at the end of my path.
So, here is my path:
Destination = "C:\Users\Méloo\Documents\Photos Chevaux\& InscriptionChevaux.NomCheval.Value & .jpg"

My problem now is that I would like the name of my photo to change with each registration, and for it to take the name of the horse. However, with this path, InscriptionChevaux.NomCheval.Value does not change, so all the photos have the same name and with each registration, the new photo overwrites the old one...
I tried to create a variable, but each time it's the same; the name does not change...
Do you have any ideas, please?
0
Johann
 
Hello,
Very interesting post, thank you.
For my part, I want to display an image (in my userform) that has an internet link (type http://) as its address.
This is just for displaying the image. No local saving.
Thank you.
0
  • 1
  • 2