VBA to copy and paste folder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need help with some code to copy a folder from one destination and paste it
to another.

Sub Transfer()

Dim copy_dir As String
Dim paste_dir As String

copy_dir = "C:\test_source"
paste_dir = "C:\test_destination"

FileCopy copy_dir, paste_dir

It seems that filecopy is not the answer, I need something like foldercopy.
Is there a VBA command to copy and paste folders and contents similr to
filecopy?

Thanks
Ronaldo
 
Try the following:

FileSystemObject.CopyFolder copy_dir, paste_dir

Regards,

Alasdair Stirling
 
Alastair,

Tried using FileSystemObject.CopyFolder copy_dir, paste_dir but gor run time
424 error - object required - do I need to declare something??

Thanks

Ronaldo
 
Dim fso as Object
set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFolder copy_dir, paste_dir
 
To use the FileSystemObject, you need to set a reference (VBA,
Tools menu, References) to the "Microsoft Scripting Runtime"
library. Then, declare a variable with a FileSystemObject type,
and use that variable.

Dim FSO As Scripting.FileSystemObject
FSO.CopyFolder old_folder, new_folder, True



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
I forgot the line of code to create the FSO.

Dim FSO As Scripting.FileSystemObject
Set FSO = New Scripting.FileSystemObject
FSO.CopyFolder old_folder, new_folder, True


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Just some added explanation because of Chip's response.
My method uses late binding and doesn't require the reference as Chip has
stated. His method (early binding) would be faster since creating the
reference provides more information to Excel VBA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top