using VBA to copy a file

  • Thread starter Thread starter Paul Ponzelli
  • Start date Start date
P

Paul Ponzelli

I would like to use VBA to copy a file from one directory to another.
Assuming the parameters are:

filename: myFile.xls
source directory: P:\folder1\
target directory: Q:\folder2\
overwrite existing file without asking

How can I do this in VBA?

Thanks in advance,

Paul
 
I found the answer to my question on one of the Web formus and it works
great, so I thought I would share it with the group:

Dim fs As Object
Dim oldPath As String
Dim newPath As String
Dim myFile As String

oldPath = "P:\folder1" 'Folder file is located in
newPath = "Q:\folder2" 'Folder to copy file to
myFile = "myFile.xls"

Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & ppWorkPlan, newPath & "\" & ppWorkPlan

Set fs = Nothing

here's the site that I got it from:

http://forums.devarticles.com/microsoft-access-development-49/copy-file-using-vba-30811.html

Paul
 
Paul said:
I found the answer to my question on one of the Web formus and it works
great, so I thought I would share it with the group:

Dim fs As Object
Dim oldPath As String
Dim newPath As String
Dim myFile As String

oldPath = "P:\folder1" 'Folder file is located in
newPath = "Q:\folder2" 'Folder to copy file to
myFile = "myFile.xls"

Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & ppWorkPlan, newPath & "\" & ppWorkPlan

Set fs = Nothing


There is no need to be dependent on the Scripting library.
Since Scripting is a popular method of propogating worms or
performing destructive actions, it is often removed from
user's sytems.

Try this instead:

Dim oldPath As String
Dim newPath As String
Dim myFile As String

oldPath = "P:\folder1" 'Folder file is located in
newPath = "Q:\folder2" 'Folder to copy file to
myFile = "myFile.xls"

FileCopy oldPath & "\" & ppWorkPlan, _
newPath & "\" & ppWorkPlan
 
That's a better way to copy a file with VBA.

Thanks for the illumination (again) Marsh.

Paul
 
Back
Top