Copy Folder problem (1 Viewer)

Dachande11

Registered User.
Local time
Today, 12:19
Joined
May 1, 2001
Messages
41
Hello,

I am trying to copy folders within code and thought I had the answer in the following code which I found on this forum.

Dim fs As Variant

Set fs = CreateObject("Scripting.FileSystemObject")

fs.copyfolder "C:\Documents and Settings\szymkm\My Documents\test", "C:\Documents and Settings\szymkm\My Documents\test\FolderTest"

Unfortunately I keep getting the following error. "Invalid procedure call or argument (Error 5)"

If anyone could point me in the right direction I would be very grateful.

Thanks

Mark
 

ghudson

Registered User.
Local time
Today, 07:19
Joined
Jun 8, 2002
Messages
6,195
Copy files from one directory to another directory

Try this...

Code:
'Your db must have a reference set to the "Microsoft Scripting Runtime"!
'From a module, click Tools/Reference then select "Microsoft Scripting Runtime".
'The scripting file is located @ C:\Windows\System32\scrrun.dll for Windows XP.

Private Sub bCopyDirectoryFiles_Click()
On Error GoTo Err_bCopyDirectoryFiles_Click
 
    Dim fso As Scripting.FileSystemObject
    Dim sSourceDir As String, sDestinationDir As String
    
    Set fso = New Scripting.FileSystemObject
    
    sSourceDir = "C:\Temp\*.*"
    sDestinationDir = "C:\Temp\Test" 'No ending back slash for the final directory!
    
    fso.CopyFile sSourceDir, sDestinationDir, True
    
    MsgBox "Files copied"
 
Exit_bCopyDirectoryFiles_Click:
    Exit Sub
 
Err_bCopyDirectoryFiles_Click:
    If Err = 53 Then 'File not found
        Beep
        MsgBox "No files found in the source directory.", vbInformation
        Exit Sub
    ElseIf Err = 76 Then 'Path not found
        Beep
        MsgBox "The destination directory does not already exist.", vbInformation
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_bCopyDirectoryFiles_Click
    End If
 
End Sub
 

Dachande11

Registered User.
Local time
Today, 12:19
Joined
May 1, 2001
Messages
41
Thanks very much ghudson, that works great now.

Mark
 

Dannyc989

Registered User.
Local time
Today, 12:19
Joined
Dec 18, 2013
Messages
46
Ghudson I would like to use this code but how would I get the ssourcedirectory to come from a field on my form??
 

Dannyc989

Registered User.
Local time
Today, 12:19
Joined
Dec 18, 2013
Messages
46
ok i must be doing something wrong copied and changed the bits needed but I get an error of

Compile error invalid use of Me Keyword, on the sSourceDir havent got past this so not sure if the sDestinationDir works either

here is the code as i have it at the moment in the module

Sub CopyFolder()

Dim fso As Scripting.FileSystemObject
Dim sSourceDir As String, sDestinationDir As String

Setfso = New Scripting.FileSystemObject

sSourceDir = Me.Quote_File_Directory_Ref
sDestinationDir = "C:\Users\Daniel\Desktop\NTProjects\" & [Project Number] & "_" & [Client] & "_" & [Description] 'No ending back slash for the final directory!

fso.CopyFile sSourceDir, sDestinationDir, True

MsgBox "Files copied"

End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom