Intro To Vba Fso Copyfile: Vba Copy A File Macro
Intro To Vba Fso Copyfile: Vba Copy A File Macro
The FSO CopyFile method is a quick VBA way to copy a file from one location to another. Use the VBA
CopyFile FileSystemObject (FSO) to copy a file to another folder. The FSO is part of Windows Script Host
which provides scripting abilities like batch files.
If you haven’t already done so, check out our Introduction to the VBA FileSystemObject for a
general introduction to the FSO and all its functions.
Even though copying a file sounds easy enough, there are surprisingly many potential pitfalls associated
with it. For this reason, it should be emphasized right from the outset that you should always incorporate
error handling into your code when you’re accessing a computer’s file system.
In this tutorial we’ll first present the basic setup for copying a file with the FSO CopyFilefunction and then
we’ll show you exactly how to invoke this function. After this, to help you avoid the potential pitfalls, we’ll
also present a more sophisticated procedure which includes error handling. This will ensure that you don’t
make mistakes when you’re handling your own files.
End Sub
CAUTION: The procedure above contains no safeguards (error handling) and it is not recommended to
use the functions of the FileSystemObject this way, unless you know exactly what you’re doing! Later in
this tutorial, we’ll show you an augmented CopyFileprocedure which incorporates error handling.
Sub FSOCopyFileDemo()
Call CopyFileWithFSOBasic("C:\MyFiles\Test.xlsm", "C:\MyBackup\", False)
End Sub
The first parameter, “C:\MyFiles\Test.xlsm” is the source file to be copied. Note, you must specify the full
path to the file, starting with the drive name.
The second parameter, “C:\MyBackup", is the path to the destination folder. Notice the backslash \ at the
end of the path.
Without it, the procedure will copy the file to the preceding folder in the specified destination path, in this
case the root of the C-drive, but the copied file will not have any extension!
The third parameter, False is a Boolean type variable which specifies whether or not you want to overwrite
any existing file by the same name in the destination folder. If set to False, it means you don’t want to
overwrite. When set to True, it means you do. This is very nifty, if you regularly backup the same file to a
particular folder.
If you set this parameter to False and a file by the same name already exists in the destination folder, a
run-time error will occur when you run the procedure, unless you incorporate error handling into your code.
One beauty of the FSO CopyFile function is that you can simultaneously copy a file and rename it in the
new destination. You can use this basic macro to copy a file from one location to another while renaming it
in the final destination folder by calling the CopyFile macro like this:
In the next section, we’ll show you how to deal with all kinds of error handling contingencies!
The following errors will throw an exception at run-time and cause the CopyFileWithFSOBasic procedure to
halt:
Moreover, a forgotten backslash \ at the end of the destination path will create a file with no extension in
the previous component of the destination path. This is not a run-time error, but it’s also not exactly the
result you expected! In many ways, this is worse than a run-time error because you may think the program
ran to completion, completely oblivious to the fact that it didn’t copy the file where you wanted it.
With this in mind, let’s introduce a new, more advanced, procedure which includes handling of all the
above-mentioned errors.
With FSO
' error
If blSourceErr Then _
strErrMsg = "The source file," & Chr(34) & strFileName & Chr(34) & _
" does not exist, or the specified path to the file, " & Chr(34) & _
Replace(SourceFilePath, strFileName, "") & Chr(34) & " is incorrect."
' error
Else
MsgBox strErrMsg, vbCritical, "Error!"
End If
End With
End Sub
When you execute the procedure above, you’ll either get a success message or an error message,
depending on whether or not you specified the parameters to the procedure correctly. One limitation of the
advanced CopyFileWithFSO macro is that you can’t rename the file when copying it, like you can with
the CopyFileWithFSOBasic macropresented at the top of this tutorial. That’s just a byproduct of the error
handling logic, but you can change it if you like!