Search code examples
ms-accessvbams-access-2010

Copy and paste a file through access vba


I need to check a folder for a file, if it exists delete it an replace it with an updated version, or if the file doesn't delete then it will copy the file from a path into the individuals personal drive

My Code:

Dim FileExistsbol As Boolean
Dim stFileName As String
Dim CopyFrom As String
Dim CopyTo As String

stFileName = "H:\Test File.txt"
stFileName = Trim(stFileName)
FileExistsbol = dir(stFileName) <> vbNullString

If FileExistsbol Then
 Kill stFileName
 CopyFrom = "J:\Test File.txt"
 CopyTo = "H:\"
 FileSystemObject.CopyFile CopyFrom, CopyTo
Else
 CopyFrom = "J:\Test File.txt"
 CopyTo = "H:\"
 FileSystemObject.CopyFile CopyFrom, CopyTo
End If

What Happens:

The code executes and deletes the existing file as expected, but it appears to be failing on the copy and paste part.

Error:

The debug that comes up is:

Object Required


Solution

  • Bare bones:

    Dim fso As Object       'filesystemobject    
    Set fso = CreateObject("Scripting.FileSystemObject")    
    fso.CopyFile strSourcePathWithFileAndExt, strDestPathWithFinalBackslash    
    Set fso = Nothing