PDA

View Full Version : Solved: Copying a file when the source file is open.



Jarlisle
09-20-2011, 02:44 PM
Is there a way to copy a file when the file is open?

I have the following, but it will error when the file is open:

FileCopy "U:\folder\file_name.xls", "U:\folder2\file_name.xls"

xld
09-20-2011, 02:57 PM
Const FileName1 As String = "U:\folder\file_name.xls"
Const FileName2 As String = "U:\folder2\file_name.xls"

CreateObject("Scripting.FileSystemobject").CopyFile FileName1, FileName2

Jarlisle
09-21-2011, 07:43 AM
Thanks XLD! It seems to have worked.

If I want to copy 2 files or more would I just need to duplicate the script, or would it cause issues with something?

xld
09-21-2011, 09:10 AM
No, don't duplicate it, it will instantiate a filesystemobject each time.

Rather use this approach


Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemobject")
fso.CopyFile FileName1, FileName2
fso.CopyFile FileName3, FileName4
'rest of code

Set fso = Nothing

Jarlisle
09-21-2011, 10:00 AM
Works great, thanks!