In this incomplete VBA code, everything works perfectly. the source folder is correctly…
- User
In this incomplete VBA code, everything works perfectly. the source folder is correctly identified and the destination folder is also correctly identified. Even in the MsgBox "Files found in the folder that match the pattern:" & vbCrLf & filesFound, the fileFound is correctly identified, showing the filename and the file type. I would like help completing the VBA code, to do the following. Move the fileFound from the source folder to the destination folder and after the move has been confirmed, delete the existemce of the fileFound from the source folder.
Option Explicit
Sub MoveFileQuote(ByVal TargetCell As Range) Dim folderPath As String Dim fileNamePattern As String Dim destinationFolder As String Dim fileName As String Dim fileFound As Boolean Dim objShell As Object Dim objExplorer As Object Dim obj As Object
' Define source and destination folders folderPath = ThisWorkbook.Path & "\" & "z Quotes On Hold" destinationFolder = ThisWorkbook.Path & "\" & TargetCell.Offset(0, -4).Value Set objShell = CreateObject("Shell.Application") Set objExplorer = objShell.Windows ' Open the source folder in Windows Explorer and confirm Shell "explorer.exe " & folderPath, vbNormalFocus Application.Wait (Now + TimeValue("0:00:02")) Dim confirmSource As VbMsgBoxResult confirmSource = MsgBox("Is the source folder correct?", vbYesNo + vbQuestion) ' If source folder is incorrect, exit the code If confirmSource = vbNo Then MsgBox "Please verify the source folder and run the code again.", vbInformation, "SOURCE" Exit Sub End If ' Open the destination folder in Windows Explorer and confirm Shell "explorer.exe " & destinationFolder, vbNormalFocus Application.Wait (Now + TimeValue("0:00:02")) Dim confirmDest As VbMsgBoxResult confirmDest = MsgBox("Is the destination folder correct?", vbYesNo + vbQuestion) ' If destination folder correct, proceed with file processing If confirmDest = vbNo Then MsgBox "Please verify the destination folder and run the code again.", vbInformation, "DESTINATION" Exit Sub End If fileNamePattern = "Quote " & TargetCell.Value & "*" fileFound = False Dim fso As Object Dim objFile As Object ' Display files found in the folder that match the fileNamePattern Dim filesFound As StringSet fso = CreateObject("Scripting.FileSystemObject")
If Not fso Is Nothing Then For Each objFile In fso.GetFolder(folderPath).files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then filesFound = filesFound & vbCrLf & objFile.Name fileFound = True End If Next objFile
If fileFound Then MsgBox "Files found in the folder that match the pattern:" & vbCrLf & filesFound Else MsgBox "No files found in the folder that match the pattern." End If Exit Sub' CODE WORKS ACCURATELY UP TO THIS POINT
- Assistant
' If a file that matches the pattern is found, move the file to the destination folder If fileFound Then Dim i As Integer For Each obj In objExplorer If TypeName(obj.Document) = "HTMLDocument" And InStr(obj.Document.title, "Quotes") > 0 Then obj.Document.All("Item").Click obj.Document.All("Edit").Click Exit For End If Next obj For Each objFile In fso.GetFolder(folderPath).files If InStr(1, objFile.Name, TargetCell.Value, vbTextCompare) > 0 Then If MsgBox("Are you sure you want to move " & objFile.Name & " to the destination folder?", vbYesNo + vbQuestion) = vbYes Then fso.GetFile(objFile.Path).Move (destinationFolder & "" & objFile.Name) MsgBox objFile.Name & " has been successfully moved to the destination folder."
If fso.FileExists(destinationFolder & "\" & objFile.Name) Then If MsgBox("Do you want to delete " & objFile.Name & " from the source folder?", vbYesNo + vbQuestion) = vbYes Then fso.DeleteFile objFile.Path MsgBox objFile.Name & " has been successfully deleted from the source folder." End If End If Else MsgBox "Move operation canceled." End If Exit For End If Next objFile End If