Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-30-2025, 07:21 PM
Logit Logit is offline Copy or Move Files Windows 10 Copy or Move Files Office 2007
Expert
Copy or Move Files
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default Copy or Move Files

The following macro fails to move or copy files to the destination folder. It appears to successfully copy the selected files but
fails on moving. I would appreciate your testing of the entire code and provide a solution to moving the files. Thank you in advance.



Code:
Sub MoveUserSelectedFiles()
    Dim sourceFiles As FileDialog
    Dim destinationFolder As String
    Dim filePath As String
    Dim fso As Object
    Dim i As Integer
    
    ' Initialize FileDialog for selecting files
    Set sourceFiles = Application.FileDialog(msoFileDialogFilePicker)
    sourceFiles.Title = "Select Files to Move"
    sourceFiles.AllowMultiSelect = True
    
    ' Show file selection dialog
    If sourceFiles.Show = -1 Then
        If sourceFiles.SelectedItems.Count = 0 Then
            MsgBox "No files were selected. Operation cancelled.", vbExclamation
            Exit Sub
        End If
        
        ' Initialize FileDialog for selecting the destination folder
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select Destination Folder"
            If .Show = -1 Then
                destinationFolder = .SelectedItems(1) & "\"
            Else
                MsgBox "No destination folder selected. Operation cancelled.", vbExclamation
                Exit Sub
            End If
        End With
    Else
        MsgBox "No files selected. Operation cancelled.", vbExclamation
        Exit Sub
    End If
    
    ' Initialize FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Loop through selected files and move them
    For i = 1 To sourceFiles.SelectedItems.Count
        filePath = sourceFiles.SelectedItems(i)
        
        ' Ensure the file exists before moving
        If fso.FileExists(filePath) Then
            Debug.Print "Moving: " & filePath & " to " & destinationFolder & fso.GetFileName(filePath)
            
            ' Try CopyFile first to verify access
            On Error Resume Next
            'fso.CopyFile filePath, destinationFolder & fso.GetFileName(filePath)
            fso.MoveFile filePath, destinationFolder & fso.GetFileName(filePath)

            
            If Err.Number <> 0 Then
                MsgBox "Error moving file: " & filePath & vbNewLine & "Error: " & Err.Description, vbCritical
                Err.Clear
            Else
                ' Only delete after confirming successful copy
                fso.DeleteFile filePath
            End If
            On Error GoTo 0
            
        Else
            MsgBox "File not found or inaccessible: " & filePath, vbCritical
        End If
    Next i
    
    MsgBox "Files moved successfully!", vbInformation
    
    ' Clean up
    Set fso = Nothing
    Set sourceFiles = Nothing
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dragging a selection to copy it, not to move it wardw Word VBA 2 11-28-2019 11:09 AM
VBA Word - Search Within Files Containing A String - Copy Files to New Folder jc491 Word VBA 0 01-09-2016 12:00 PM
Copy or Move Files copy and move not working inventorgeorge Word 8 07-09-2012 03:59 PM
Hyperlink i Word: Copy-paste, and move eradem Word 0 10-01-2011 03:50 AM
Copy or Move Files don't move/copy...., where? Yumin Word 1 04-04-2010 08:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft