Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystem.FileCopy vs objFSO.CopyFile

Tags:

excel

vba

Are there specific scenarios where I should use on of the following or are they interchangeable?

Option 1

...
FileSystem.FileCopy...
...

Option 2

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

Or is option 1, under the hood, just option 2 ?

like image 426
whytheq Avatar asked Oct 21 '25 11:10

whytheq


1 Answers

Option 1's FileSystem.FileCopy is the built-in VBA function to copy a file, its part of the core language and always available (you don't need the FileSystem namespace prefix)

Option 2 is more complex; it uses COM to create a late-bound FSO instance and calls a method that has the same name and does the same thing as the built-in.

There seems little benefit in using option 2 over option 1, the only functional difference is that the FSO exposes an argument to control overwrite behaviour, something you would need to deal with manually using the built in FileCopy (trivial enough).

like image 82
Alex K. Avatar answered Oct 23 '25 14:10

Alex K.