CopyFile question

G

Garry

Can someone explain why the first code works but the
second code doesn't? The second code generates a "file not
found" error.

Code that works:
Set fs = CreateObject("Scripting.FileSystemObject")
WorkingDir = "c:\nmv\run\"
fs.CopyFile "c:\nmv\run\data\datafile.csv", WorkingFile

Code that doesn't work:
Set fs = CreateObject("Scripting.FileSystemObject")
WorkingDir = "c:\nmv\run\"
DataToCopy = "c:\nmv\run\data\datafile.csv"
fs.CopyFile DataToCopy, WorkingFile

TIA,
Garry
 
P

Perry

Kindly supply some more system info or code snippets ...
Below ones are almost equal, I assume the problem
occurs elsewhere in code ...

Krgrds,
Perry
 
G

Guest

Perry,

Other information. The macro was written in Word 2000
running on a Windows 98 platform

I plan to use this method is a larger macro but this is a
separate macro that I wrote to test copying a file. Except
for some minor code to assign filename to the DataToCopy
variable, this is the entire code. When I use as MsgBox to
confirm the string in the DataToCopy variable, it checks
out OK in all cases.

I subsequently discovered another interesting bit. As I
noted, if you place the actual string in the CopyFile
command it works but if you assign the string to a variable
then use the variable in the CopyFile command it doesn't
work. Well, if you actually assign the string as follows:

DataToCopy = "c:\nmv\run\data\datafile.csv"
and then use the variable DataToCopy in the CopyFile
command, it works. However, the following code does not work:

Set fs = CreateObject("Scripting.FileSystemObject")
myname="myFileName"
NewName=myname & "_01.csv"
WorkingDir = "c:\nmv\run\"
fs.CopyFile NewName, WorkingFile

No file is copied and file not found error occurs.

So, to summarize. If a direct string (as "string") is put
into the CopyFile command or the same string is directly
saved to a variable which is subsequently used in the
CopyFile command then the command functions properly. But
if you indirectly place a string into a variable by first
assigning a string to a first variable then creating a
second variable by concatination, using the second variable
in the CopyFile command doesn't function and an error
message is generated.

It appears tha VBA handles the string assignments
differently and the CopyFile differentiates between the
assignment methods.

Incidently, this exact same situation occurs with the
FileCopy command.
 

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