Hello all
I am new here and would like help with macro that i cannot seem to find why it is not working with me anymore.
So this Macro is used to locate specific files that are in orders sheet, files are normally images or PDF files saved within a specific folders or path, the problem is that i only want those on Orders sheet to be searched for in a path and put into a new folder, but this is not working now for me, and i dont know how to fix it. the results that are found normally gets highlighted in the orders sheet but this has all stopped now. and it is frustrating me that when it launches it stops but the folder gets created without any results in there.
Public Sub HCSyncreonPOD()
Dim DT As String
Dim Source As String
Dim Dest As String
Dim vFiles As Variant
Dim vFile As Variant
Dim rCell As Range
Dim oFSO As Object
Dim FileFound As Boolean
Dim FF As Long
On Error Resume Next
FF = FreeFile
DT = Format(Now, "dd.mm.yyyy")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Source = "\\CZCHONYSVM007.prg-dc.dhl.com\BS50769_Galactica\Syncreon_Hard_Copies\SYNCREON HARD COPIES"
Dest = "\\CZCHONYSVM007.prg-dc.dhl.com\BS50769_Galactica\Syncreon_Hard_Copies\HC-POD Verbal Found on " & DT
If Dir(Dest, vbDirectory) = "" Then MkDir Dest
'Get the full path name of all PDF files in the source folder and subfolders.
vFiles = EnumerateFiles(Source, "*")
With Worksheets("Orders")
'Look at each cell containing file names.
For Each rCell In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
FileFound = False 'Assume the file hasn't been found.
'Check each value in the array of files.
For Each vFile In vFiles
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Use this line if the file name in the sheet exactly match the file name in the array. '
'8152 and 100_8152.pdf are not a match, 8152 and 8152.pdf are a match. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If rCell & ".pdf" = FileNameOnly(vFile) Then
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Use this line if the file name in the sheet should appear in the file name in the array. '
'8152 and 100_8152.pdf are a match, 1852 and 8152.pdf are a match. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If InStr(FileNameOnly(vFile), rCell.Value) > 0 Then
'If found copy the file over and indicate it was found.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This line will use the rcell value to name the file. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'oFSO.CopyFile vFile, Dest & "" & rCell & "*.pdf"
''''''''''''''''''''''''''''''''''''''
'This line will not rename the file. '
''''''''''''''''''''''''''''''''''''''
On Error Resume Next
oFSO.CopyFile vFile, Dest & "" & FileNameOnly(vFile)
rCell.Interior.Color = RGB(250, 255, 25)
FileFound = True
End If
Next vFile
'Any file names that aren't found are appended to the text file.
'If Not FileFound Then
' Open (Dest & "" & "MissingFiles.txt") For Append As #FF ' creates the file if it doesn't exist
' Print #FF, rCell ' write information at the end of the text file
' Close #FF
'End If
Next rCell
End With
Sheets("Control").Select
MsgBox "Transfer complete, You got all Syncreon HC POD`s :)"
End Sub
Public Function EnumerateFiles(sDirectory As String, _
Optional sFileSpec As String = "*", _
Optional InclSubFolders As Boolean = True) As Variant
EnumerateFiles = Filter(Split(CreateObject("WScript.Shell").Exec _
("CMD /C DIR """ & sDirectory & "*." & sFileSpec & """ " & _
IIf(InclSubFolders, "/S ", "") & "/B /A:-D").StdOut.ReadAll, vbCrLf), ".")
End Function
Public Function FileNameOnly(ByVal FileNameAndPath As String) As String
FileNameOnly = Mid(FileNameAndPath, InStrRev(FileNameAndPath, "") + 1, Len(FileNameAndPath))
End Function
Bookmarks