copying file from one location to another using vb code

'I basically want to copy a file to another location using vb code..... yes very simple but I cant get
it to work.

'Here is so code I am currently using.. Im storing the file name and location in a string and was expecting
to copy this to another location.

Public function CopyFile()

Dim strOldFile As String
Dim strFileDest As String

With frmAssessment.dlgImage
   .FileName = ""
End With

strFileDest = frmAssessment.dlgImage.FileName

FileSystemObject.CopyFile strFileDest, "c:\database\"

Call RenameFile(strOldFileName, strNewFileName)  

End function

'If you have anyother ideas how I can copy a file to a new location plz reply. The File system object bring up a error message (object required) error 424'  
Who is Participating?
sombellConnect With a Mentor Commented:
you need to declare the filesystems object.


dim fso as new filesystemobject
fso.copyfile strFileDest,"c:\database\"

Hope this helps,

Ryan ChongCommented:
Use Copyfile in VB:

CopyFile source, destination[, overwrite]


CopyFile source, destination

Ryan ChongCommented:
I mean you don't need to declare filesystemobject
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Yakeni, yes, as sombell points out, the 'FileSystemObject' is an object like any other, you need to explicitly create it, it is not an intrinsicly global object in VB (such as 'Debug' or 'VB' or 'Printer')
what CopyFile in VB ? what copyfile would that be ?

This code:

Private Sub Form_Load()
copyfile "c:\test.doc", "c:\test1.doc"
End Sub

fails at compile time.
You can try FileCopy function in FileSystem class (build in VB)
FileCopy SrcPath,DstPath
FileCopy "C:\test.txt","c:\Data"
Some related usefull function in that class
Name --> remname file
Dir --> navigate files and folders
Kill --> delete file
Ryan ChongCommented:
ya, it's my careless, i mean "FileCopy" as thuannc posted. Apologies.
YakeniAuthor Commented:
Thanks for that> I didnt know you had to declare a filesystemobject. Your code was close.

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
actually, you dont need to do that....

if you add the reference to the microsoft scripting runtime to the project then

Dim fso as new FileSystemObject
will work, and then you dont need the CreateObject.

Its more efficient to do it this way as it uses Earlu Binding.

CreateObject uses Late Binding

But both will work.


I had assumed you already had the reference included.

(oh, and thats Sombell) :o)

happy to have helped
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.