+ Reply to Thread
Results 1 to 5 of 5

Copy a folders & files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-16-2012
    Location
    Halifax, UK
    MS-Off Ver
    MS Office 365
    Posts
    206

    Copy a folders & files

    Hi
    I am trying to get a master purchase order sheet to copy and rename a set of folders and files contained within to a central location.
    The folders are in the 'Standard Documents Folder' and I am wanting these to copy to the main 'Contract' Folder on the server (at the minute I'm using a test folder 'Test').
    The code I have below so far is:

    Option Explicit
    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        Dim wkb As Object
      
        FromPath = "K:\APPS\Standard Document Files\Contracts\New Contract Folder"  '<< Change
        ToPath = "K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5")  '<< Change
        wkb.SaveAs Filename:="K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5") & "\Purchase Order" & Range("A3") & "Purchase Order" & ".xlsb"
        
        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    
        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    
        Set FSO = CreateObject("scripting.filesystemobject")
    
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
    
        FSO.CopyFolder Source:=FromPath, Destination:=ToPath
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    
    End Sub
    I am getting the error "Object Variable or With Block Variable not set" on the following code:

    wkb.SaveAs Filename:="K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5") & "\Purchase Order" & Range("A3") & "Purchase Order" & ".xlsb"
    Can anyone see why?

    My next query when I can get this one sorted is to have the other excel files that are already on the copied files to be renamed in the same way (contract number etc, so RAMS would become C12345 RAMS etc).
    What would be the best way to do this?
    Kieran

  2. #2
    Forum Contributor
    Join Date
    03-16-2012
    Location
    Halifax, UK
    MS-Off Ver
    MS Office 365
    Posts
    206

    Re: Copy a folders & files

    I've got it to work and save the master workbook as the cell values, I just need to sort the correct name and folder destination out now!

    Option Explicit
    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        
        FromPath = "K:\APPS\Standard Document Files\Contracts\New Contract Folder"  '<< Change
        ToPath = "K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5")  '<< Change
      
        
        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    
        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    
        Set FSO = CreateObject("scripting.filesystemobject")
    
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
        
        FSO.CopyFolder Source:=FromPath, Destination:=ToPath
        MsgBox "You can find the files and subfolders in " & ToPath
        
    ActiveWorkbook.SaveAs Filename:="K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5") & "\Purchase Order" & Range("A3") & "Purchase Order" & ".xlsb"
    End Sub

  3. #3
    Forum Contributor
    Join Date
    03-16-2012
    Location
    Halifax, UK
    MS-Off Ver
    MS Office 365
    Posts
    206

    Re: Copy a folders & files

    Ahh I'm still getting the same error message
    "Run-time error '1004' Method 'SaveAs' of object'_workbook failed"

    Option Explicit
    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
    
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        
        FromPath = "K:\APPS\Standard Document Files\Contracts\New Contract Folder"  '<< Change
        ToPath = "K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5")  '<< Change
      
        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    
        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    
        Set FSO = CreateObject("scripting.filesystemobject")
    
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
            
    Dim Path1 As String
    Dim Path2 As String
    Dim myfilename As String
    Dim fpathname As String
    
    Path1 = ToPath
    Path2 = ToPath & "\Purchase Orders"
    myfilename = Range("A3") & "DCS Purchase Order" & ".xlsb"
    fpathname = Path1 & "\" & Path2 & "\" & myfilename & ".xlsb"
    ActiveWorkbook.SaveAs Filename:=fpathname, _
    FileFormat:=xlExcel12, CreateBackup:=False
        
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders in " & ToPath & "." & " " & "This Purchase Order sheet has been saved in the Purchase Orders folder"
        
    End Sub

  4. #4
    Forum Contributor
    Join Date
    03-16-2012
    Location
    Halifax, UK
    MS-Off Ver
    MS Office 365
    Posts
    206

    Re: Copy a folders & files

    I have played around a bit more and can get this to work when I "note" out the SaveAs code, press my button, go back into the code and remove the ' from the SaveAs code and then press the button again.
    Looking around on here and other sites, it is obviosuly giving the error as it can't find the folder I am asking the code to create and the save the file into this new folder.

    Is there a way around this?
    Kieran

    ActiveWorkbook.SaveAs Filename:=Name, _
    FileFormat:=50, CreateBackup:=False


    Option Explicit
    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
    
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
        
        FromPath = "K:\APPS\Standard Document Files\Contracts\New Contract Folder"  '<< Change
        ToPath = "K:\APPS\CONTRACT\Test\" & Range("A3") & " " & Range("A5")  '<< Change
      
        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    
        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    
        Set FSO = CreateObject("scripting.filesystemobject")
    
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
        
        End If
           
            
    Dim Path1 As String
    Dim myfilename As String
    Dim Name As String
    
    Path1 = ToPath & "\Purchase Orders"
    myfilename = "C" & Range("A3") & " " & "DCS Purchase Order" & ".xlsb"
    Name = Path1 & "\" & myfilename & ".xlsb"
    ActiveWorkbook.SaveAs Filename:=Name, _
    FileFormat:=50, CreateBackup:=False
        
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders in " & ToPath & "." & " " & "This Purchase Order sheet has been saved in the Purchase Orders folder"
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-16-2012
    Location
    Halifax, UK
    MS-Off Ver
    MS Office 365
    Posts
    206

    Re: Copy a folders & files

    Anyone got any ideas?

    I have taken the SaveAs code out of the main code section now, put this into a separate module. Then added the call macro code to make this run within the main code section.

    It still gives the error as the SaveAs tries to run before the folders are created in the first section of code. How can I get around this?

    Kieran

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy multiple files in different folders
    By payalvj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-15-2016, 06:57 AM
  2. Use Excel VBA to Copy multiple files from different source folders to different folders
    By mm1234mail in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2014, 01:17 PM
  3. Replies: 0
    Last Post: 05-26-2014, 07:47 PM
  4. How to copy and rename files in vba by searching multiple folders for files
    By razorace in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2014, 04:21 AM
  5. Replies: 1
    Last Post: 09-12-2013, 09:23 PM
  6. Create folders and move and copy files into that folders
    By vijaybharthi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-31-2010, 04:01 AM
  7. Copy multiple files from folders
    By jeff p in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-30-2009, 02:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1