Copy a file with command button using code?

  • Thread starter Thread starter scs
  • Start date Start date
S

scs

I'm looking for some simple code to copy a file by clicking on a command
button. I'd like to copy C:\Documents and Settings\All Users\Documents\IBI
Update\filename.mdb to \\Inspiron700m\shareddocs\filename.mdb. The file
will not be in use. The file will exist so I need to overwrite. Thanks for
any help. If possible could you please include error handling?

TIA
Steve
 
Look at the CopyFile at the end of the sub - the "True" is for overwrite (see
VBA help on CopyFile for more details). Sorry - the error-trapping is pretty
light in this example - it just displays the error code & description.

Private Sub Button1_Click()
On Error GoTo ErrorHandler
Dim FileSysObj As Object
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
Dim OldFileName As String
Dim NewFileName As String
OldFileName = "C:\Documents and Settings\All
Users\Documents\IBIUpdate\filename.mdb"
NewFileName = "\\Inspiron700m\shareddocs\filename.mdb"
FileSysObj.CopyFile OldFileName, NewFileName, True
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & ": " & Err.Description
End Sub
 
Thanks very much. Works perfect.

Brian said:
Look at the CopyFile at the end of the sub - the "True" is for overwrite
(see
VBA help on CopyFile for more details). Sorry - the error-trapping is
pretty
light in this example - it just displays the error code & description.

Private Sub Button1_Click()
On Error GoTo ErrorHandler
Dim FileSysObj As Object
Set FileSysObj = CreateObject("Scripting.FileSystemObject")
Dim OldFileName As String
Dim NewFileName As String
OldFileName = "C:\Documents and Settings\All
Users\Documents\IBIUpdate\filename.mdb"
NewFileName = "\\Inspiron700m\shareddocs\filename.mdb"
FileSysObj.CopyFile OldFileName, NewFileName, True
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & ": " & Err.Description
End Sub
 

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

Back
Top