Problem copying a file with FileSystemObject.CopyFile

K

Keith

I am trying to copoy one file to another using VBA in a module. I read the
help on the topic and saw the example for copyfile method. The example is
.....

FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"

Now, when I try the code I get an "object required" error.

I've copied and pasted the two file paths, so I am confident they are
correct, and the originating file is present. What likely going on?

Below is my code, although pasting it in here, there are some line-breaks
that were generated. In the original, it's all on one line.

FileSystemObject.CopyFile "C:\Documents and
Settings\mydirectory\Copy_Test\Copy_Test_Sub_Folder_1\*.doc", "C:\Documents
and Settings\mydirectory\Copy_Test\test.doc", True

in copy_test_sub_folder_1 I also tried naming the exact file "test.doc"

Thank you.
 
P

Perry

Try

Set a reference in VBE to:
Microsoft Scripting Runtime

Dim fso as New FileSystemObject
fso.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"

Krgrds,
Perry
 
K

Ken Snell \(MVP\)

Or use late binding without setting a reference to the scripting library:

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

This avoids the overhead of having a permanent reference to this library.
 

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

Top