+ Reply to Thread
Results 1 to 10 of 10

Error Handling - Copy Folder VBA

Hybrid 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.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Error Handling - Copy Folder VBA

    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 6)
    - Battle without fear gives no glory - Just try

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

    Re: Error Handling - Copy Folder VBA

    Thank you, I updated that.

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Error Handling - Copy Folder VBA

    if I click cancel on the dialog box
    which dialogbox ? what is the message with the dialogbox

    It seems thet the dialogbox used are only for display no input

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

    Re: Error Handling - Copy Folder VBA

    The save as dialog box. If I click cancel at that point it still copies the folder with the name false.

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Error Handling - Copy Folder VBA

    To avoid misleading can you open the macro and go step by step ( using key F8) and mention when you are asked to save with Cancel option

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Error Handling - Copy Folder VBA

    Got it ... perhaps
    see next code

        FromPath = "C:\Dropbox\CUSTOMER_TEMPLATE_FOLDER"  '<< Change
        ToPath = Application.GetSaveAsFilename
        If (ToPath = "False") Then Exit Sub        ' Statement to add

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

    Re: Error Handling - Copy Folder VBA

    Fantastic! Works like a charm! Someday I will be up to this Jedi level.......until then I will be the one asking questions and not responding to them.

    Thank you!

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

    Re: Error Handling - Copy Folder VBA

    The save as dialog box opens right after the ToPath = Application.GetSaveAsFilename step. I may have something misplaced here? Fairly new to VBA......appreciate you responding.
    Capture.JPGCapture2.JPG

  10. #10
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Error Handling - Copy Folder VBA

    I will be up to this Jedi level
    no, still not at the guru and still learning ...!

+ 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 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. 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