Hi folks, am stuck with error and am not able to copy the files to the "DestinationPath" could you check and let me what is the error in this code.
>>> use code - tags <<<
>>> use code - tags <<<
Code:
Dim fso As Object
Sub CopyFilesFromSubfolders()
Dim LatestFile As String
Dim LatestDate As Date
Dim SourcePath As String
Dim DestinationPath As String
SourcePath = "D:\TTC"
DestinationPath = "D:\Design\"
Set fso = CreateObject("Scripting.FileSystemObject")
LatestFile = ""
Call FindLatestPdf(fso.GetFolder(SourcePath), LatestFile, LatestDate)
If LatestFile <> "" Then
fso.copyFile LatestFile, DestinationPath & fso.GetExtensionName(LatestFile)
MsgBox "Copied: " & fso.GetFileName(LatestFile), vbInformation
Else
MsgBox "No PDF files found.", vbExclamation
End If
End Sub
Sub FindLatestPdf(fld As Object, ByRef LatestFile As String, ByRef LatestDate As Date)
Dim fsofile As Object
Dim fsofol As Object
Dim CurrentFileDate As Date
For Each fsofile In fld.Files
If LCase(fso.GetExtensionName(fsofile)) = "r-001-002.pdf" Or _
UCase(fso.GetExtensionName(fsofile)) = "r-001-002.STEP" Or _
LCase(fso.GetExtensionName(fsofile)) = "r-001-002.dwg" Then
CurrentFileDate = fsofile.DateLastModified ' or DateCreated
If CurrentFileDate > LatestDate Then
LatestDate = CurrentFileDate
LatestFile = fsofile.Path
End If
End If
Next
' Recursively check subfolders
For Each fsofol In fld.SubFolders
Call FindLatestPdf(fsofol, LatestFile, LatestDate)
Next
End Sub
Last edited by a moderator: