Вы находитесь на странице: 1из 4

4/7/2015

Excel VBA Picture Viewer Project: Copy an image to a folder

Excel VBA Programming


Home
Getting Started
8 part section >>
VBA Programming Variables
6 Part Section >>
Conditional Logic
9 part section >>
Strings and String Functions
8 Part Section >>
Programming Loops
4 Part Section >>
Programming Arrays
4 Part Section >>
Subs and Functions
6 Part Section >>
Excel VBA and Text Files
2 Part Section >>
Excel VBA and User Forms
5 part section >>
An Excel Picture Viewer Project
Excel Picture Viewer
Design the User Form
Option and Command Buttons
Add New Photo Tab
Form Initialize Event
Get Textbox data
VBA Option Button Code
ImageBox VBA Code
Next and Previous Buttons
Add New Photo Button
Save New Image
Copy an Image with Excel VBA
Excel VBA and Charts
4 part section >>
A TreeView Project
A 6 part section >>

http://www.homeandlearn.org/copying_images.html

1/4

4/7/2015

Excel VBA Picture Viewer Project: Copy an image to a folder

Copying images to a folder


Ongoing tutorial - First part is here: Part One

If you want to copy a file you can use the FileCopy method. This method needs a source and a
destination. The source is the location on your computer where the file is; the destination is which
folder you want to copy it to.
Set up the following three variables in your code:
Dim ImageFolderPath As String
Dim SourceImage As String
Dim DestinationImage As String
We can get the destination folder by calling our NavigateFromWorkBookPath function again and
adding the images folder:
ImageFolderPath = NavigateFromWorkBookPath( )
ImageFolderPath = ImageFolderPath & "images\"
We can also add the name of the image to the end of the file path above:
DestinationImage = ImageFolderPath & ImageName
The ImageName variable is the one we set up earlier, in the General Declarations area.
We also set the location of the new image in the General Declarations area, in the CopyImage
variable:
http://www.homeandlearn.org/copying_images.html

2/4

4/7/2015

Excel VBA Picture Viewer Project: Copy an image to a folder

SourceImage = CopyImage
We don't really need to set up a new variable called SourceImage, but it's there just to make things
clearer when we copy the file.
When we copy the image over, we can test to see if the folder already contains an image of this name.
If SourceImage < > DestinationImage Then
FileCopy SourceImage, DestinationImage
End If
The If Statement checks to see if SourceImage does not equal DestinationImage. If it does then we
know the file already exists in the images folder. If they are not equal then we can go ahead and copy
it:
FileCopy SourceImage, DestinationImage
The FileCopy method needs the path of the source file first. After a comma, you type the destination
file path. We have both of these in our SourceImage and DestinationImage variables.
If you like, you can add a message at the end, just to show that everything went OK:
MsgBox "New Image Information Added"
The whole of the code for the cmdSave button looks like this:

http://www.homeandlearn.org/copying_images.html

3/4

4/7/2015

Excel VBA Picture Viewer Project: Copy an image to a folder

And that's it - the whole programme done! Test it out. Run your form and select a new image. Click
your Save New Details button and then return to your spreadsheet. You should find that a new row has
been added.

This has been a long project. But it should have given you lots of experience at creating and coding
user forms. There's obviously a lot more to them, but experiment for yourself and it should bring your
VBA skills on a treat!

In the next section of this course, we move on and look at how to add charts with VBA. You'll also learn
how to add a chart to an Excel User Form

Creating Chart with Excel VBA >


Lots more free online course here on our main Home and Learn site
All course material copyright Ken Carney

http://www.homeandlearn.org/copying_images.html

4/4

Вам также может понравиться