![]() |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
Here is a completely reworked version of the macro that successfully works :
Code:
Option Explicit
Sub myMacro()
Dim myFile As Object
Set myFile = CreateObject("Scripting.FileSystemObject")
Call myFile.CopyFile("C:\Users\logit\OneDrive\Desktop\Excel Repair\WinZip Registration.txt", "C:\Users\logit\OneDrive\Desktop\", True)
End Sub
Sub killFile()
Dim killFile As String
killFile = "C:\Users\logit\OneDrive\Desktop\Excel Repair\WinZip Registration.txt"
Dim Msg As String, Title As String
Dim Config As Integer, Ans As Integer
Msg = "Are You Sure ?"
Msg = Msg & vbNewLine & vbNewLine
Msg = Msg & "Changes Cannot Be Undone."
Title = "File Delete Critical !"
Config = vbYesNo + vbCritical
Ans = MsgBox(Msg, Config, Title)
If Ans = vbYes Then
If Len(Dir$(killFile)) > 0 Then
SetAttr killFile, vbNormal
Kill killFile
Else
MsgBox "File Not Found"
End If
End If
If Ans = vbNo Then Exit Sub
End Sub
|
|
#3
|
|||
|
|||
|
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
|
|
|
|
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 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 |
don't move/copy...., where?
|
Yumin | Word | 1 | 04-04-2010 08:22 PM |