Wildcard doesn't work in copyfile method

J

Jarryd

Hi,

Why is it that this generates the error "Object required, Error 424":

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

It's straight out the VBA helpfile. I have created those exact paths and
filename. I want to copy a file that will have a wildcard extension to
folder and then rename it. This really should be easy. The only way it
works is if I get rid of the "*". But that is ridiculous. I am currently
using a batch file to do it, but I would prefer to use VBA so that I can
easily return a meaningful error message if the file is missing.

TIA,

Jarryd
 
W

Wayne Morgan

The CopyFile command isn't VBA, it is VB Script. You can combine the
FileCopy VBA command with the Dir command to do what you're wanting using
VBA. FileCopy won't accept the wild card, but Dir will.

Example:
strFileName = Dir("c:\mydocuments\letters\*.doc")
Do Until strFileName = ""
FileCopy "c:\mydocuments\letters\" & strFileName, "c:\tempfolder\" &
strFileName
strFileName = Dir
Loop

I was able to get the following to work using the FileSystemObject:

Public Sub TestVBScript()
Dim fs As FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile "c:\batch\*.bat", "c:\test\"
Set fs = Nothing
End Sub

However, I had to set a Reference (in the code editor go to
Tools|References) to "Microsoft Scripting Runtime" first.

The example you gave is directly from the Help file. However, the Help file
says that "FileSystemObject" is the name of >A< FileSystemObject, not that
you use that word directly. You need to create the file system object first.
This is explained under the help topic "FileSystemObject Object".
 

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