Excel VBA Picture Viewer Project - Adding A New Image
Excel VBA Picture Viewer Project - Adding A New Image
http://www.homeandlearn.org/add_new_photo.html
1/7
4/7/2015
Excel
Data
Analysis
5 Ways to
Enhance
Excel Data.
Download
the Free
Whitepaper
What we'll do for the Add New Picture button is to display an Open File dialogue box so that you can
select a new image. The new image will then be loaded into the ImageBox control on the form. We'll
also put the image name into the tbImageName text box.
To make a start, select your Add New Photo tab at the top of your form. Now double click your
cmdAddNew button to open up a code stub.
Before we look at the Open File dialogue box, add a new variable to the General Declarations area.
Add the following:
Dim CopyImage As String
Your General Declarations area should now look like this:
The reason we need this variable here is because we're going to copy the selected image over to the
images folder. The image folder, remember, is where we store all the pictures referred to on the
spreadsheet. The image you want to add might be in a different location. So copying your chosen
image over to the images folder ensures that all your pictures are in one place.
http://www.homeandlearn.org/add_new_photo.html
2/7
4/7/2015
http://www.homeandlearn.org/add_new_photo.html
3/7
4/7/2015
To the right of the pipe character you type an asterisk (*). This means all file names. You then need a
dot followed by the type of file you want to display, which is jpg in this case.
To add more file types to the dropdown list, you repeat the process:
|GIF Images|*.gif
Notice that there is now another pipe character, at the very start. This is used to separate each file
type.
If we wanted to specify files of any type and any name, we'd do this:
"JPEG Images|*.jpg|GIF Images|*.gif|BITMAPS|*.bmp|All Files|*.*"
Notice the symbols used for All Files - two asterisks separated by a dot.
One final point to make is that all of your filters need to go between two sets of double quotes.
The Open File dialogue box, however, doesn't actually open a file: it just gets you a file name. This is
done with the Filename property:
fName = CD1.Filename
Because you're trying to read a value, you need the Filename property to the right of an equal sign. To
the left of the equal sign is the name of the variable that's going to store the file name.
With all that in mind, add the following code to your button:
CD1.InitDir = FilePath
CD1.DialogTitle = "Get Image File Name"
CD1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|BITMAPS|*.bmp"
CD1.ShowOpen
Dim fName As String
fName = CD1.Filename
Your coding window should now look like this (we've added some comments):
32 but users can now move on by clicking here: Get the Image Name.
http://www.homeandlearn.org/add_new_photo.html
4/7
4/7/2015
5/7
4/7/2015
6/7
4/7/2015
If you click the Cancel button on the Open File dialogue box then the file name will be blank. The If
Statement checks for that. If it's not blank then we extract the image name with these two lines:
SlashPos = InStrRev(fName, "\")
ImageName = Mid(fName, SlashPos + 1)
First, we get the position of the last backslash (\). This is then used in the Mid string method. Between
the round brackets of Mid we have the string we're trying to chop, which is in the fName variable. After
a comma, we then need a starting point to start chopping text. The starting point is the position of the
backslash plus 1. Because we haven't specified an end position, Mid will chop to the last character in
the fName string. This will get us the name of the image, which we stored in the ImageName variable.
The only other thing we need to do is to load the image into the ImageBox on the form. Here's the
code for you to add:
If Dir(fName) < > "" Then
Image2.Picture = LoadPicture(fName)
Image2.PictureSizeMode = 3
CopyImage = fName
cmdSave.Enabled = True
tbImageName.Text = ImageName
Else
MsgBox "Could not load image - no such file"
End If
This is more or less the same as we did before, for the Load Image Information button. We're doing
two things differently: one, we switch on the Save button by setting the Enabled property to True; two,
we place the image name into the tbImageName text box.
There is one line in the code above that may seem a little odd. This one:
CopyImage = fName
What this line does is to store a copy of the file name in the CopyImage variable. This is the variable
you set up in the General Declarations area. We'll need this file name when we copy the picture to the
images folder. This is done when the Save New Details button is clicked. If we made CopyImage
local to the cmdAddNew button then the cmdSave button wouldn't be able to see it.
Try it out. Run your form and click the Add New Image button. Select any image from your computer.
When you click the Open button on the dialogue box you should find that the image appears on your
form, along with the image name in the text box.
In the next lesson below, we'll write the code that saves the new image details to the spreadsheet.
http://www.homeandlearn.org/add_new_photo.html
7/7