View Single Post
 
Old 05-06-2025, 07:51 PM
Logit Logit is offline Windows 10 Office 2007
Expert
 
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

The following macro works for me :

Code:
Sub CopyOrMoveFiles()


Dim fd As fileDialog
Dim selectedItems As FileDialogSelectedItems
Dim selectedFile As Variant
Dim destinationPath As String
Dim userChoice As Integer
Dim fso As Object

    ' Initialize FileDialog
    Set fd = Application.fileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.Title = "Select Files to Copy or Move"
    
    ' Show the FileDialog
    If fd.Show = -1 Then
            Set selectedItems = fd.selectedItems
    Else
            MsgBox "No files were selected."
        Exit Sub
    End If
    
    
    ' Ask user for destination folder
    Set fd = Application.fileDialog(msoFileDialogFolderPicker)
    fd.Title = "Select Destination Folder"
    If fd.Show = -1 Then
            destinationPath = fd.selectedItems(1) & "\"
        Else
            MsgBox "No destination folder was selected."
            Exit Sub
    End If
    
    
    ' Ask user whether to copy or move files
    userChoice = MsgBox("Do you want to copy the files? Click 'No' to move them.", vbYesNoCancel + vbQuestion, "Copy or Move Files")
    If userChoice = vbCancel Then
        MsgBox "Operation cancelled."
        Exit Sub
    End If
    
    
    ' Initialize FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Copy or move files based on user choice
    For Each selectedFile In selectedItems
        If userChoice = vbYes Then
            ' Copy files
            fso.CopyFile Source:=selectedFile, Destination:=destinationPath, OverWriteFiles:=True
        Else
        ' Move files
        End If
    Next selectedFile
    
    
    MsgBox "Files have been successfully processed."


End Sub
Reply With Quote