I have been looking for codes on the internet and writing some myself to open the latest pdf file in a sharepoint folder. The files that I am interested in the folder are all named as such "SD Progress_YYYYMMDD.pdf". So I tried having a for loop through all the files in this folder and comparing the YYYYMMDD in each file names and keeping the highest value (classic max value programming). Unfortunately I am quite new with vba and I believe that I have a mistake with string or array dimensions in my code below but I can't quite figure it out. The following error occurs at the first If statement:
Run-time error '13': Type mismatch
You guys are the experts so if you have any advices for my code below please I am very interested. Thank you
CODE BELOW HAS BEEN EDITED AND WORKS NOW. THANK YOU.
Sub Shop_Drawing_Status()
Dim MyPath As String
Dim LatestDate As Integer
Dim MyFile As String
MyPath = "C:\Users\Documents...etc\"
MyFile = Dir(MyPath & "*.pdf", vbNormal)
While Len(MyFile) > 0
If Right(MyFile, 3) = "pdf" Then
LatestFile = Split(MyFile, ".")
If Right(LatestFile(0), 4) > LatestDate Then
LatestDate = Right(LatestFile(0), 4)
End If
End If
MyFile = Dir()
Wend
ActiveWorkbook.FollowHyperlink (MyPath & "SD Progress_2020" & LatestDate & ".pdf")
On Error Resume Next
End Sub