Results 1 to 10 of 10

Error Handling - Copy Folder VBA

Threaded View

  1. #1
    Registered User
    Join Date
    09-16-2018
    Location
    Washington State, USA
    MS-Off Ver
    2016
    Posts
    43

    Error Handling - Copy Folder VBA

    I grabbed some VBA online and made a few small edits. The VBA copies a specified folder and allows me to rename and save. Problem is if I click cancel on the dialog box it still saves as a "FALSE". I need it to not overwrite an existing file and also exit the sub if cancel is clicked.

    Sorry, a little new and still learning. Any help would be greatly appreciated!
    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
    
    MsgBox "CREATE NEW CUSTOMER FILE"
    
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "cmd /c mkdir C:\Dropbox\JESSE TESTING" & UserName
    
        FromPath = "C:\Dropbox\CUSTOMER TEMPLATE FOLDER"  '<< Change
        ToPath = Application.GetSaveAsFilename
        
        
        'If you want to create a backup of your folder every time you run this macro
        'you can create a unique folder with a Date/Time stamp.
        'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
    
        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 "CUSTOMER TEMPLATE HAS BEEN COPIED AND SAVED AS YOUR NEW CUSTOMER"
    
    End Sub
    Last edited by jsherwood; 09-21-2018 at 02:32 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy file from one folder to other - runtime error 53
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-16-2017, 02:49 AM
  2. Error handling inside error handling
    By grantastley in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2015, 03:43 AM
  3. [SOLVED] Error handling when fail to copy file to destination folder
    By mm1234mail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-14-2014, 04:59 AM
  4. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  5. Error Handling on Copy Sheets
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-06-2011, 01:26 PM
  6. [SOLVED] Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  7. Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

Tags for this Thread

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