Jun 30 2022 02:07 AM - edited Jun 30 2022 02:08 AM
Hello,
I am writing a macro and want to include some data from a file on Sharepoint. The macro is run daily, but a new Sharepoint file is added weekly.
So I want the macro to find the most recent file in the sharepoint folder. I have tried the code below but keep getting a runtime error (52: Bad file name or number). Can anyone point out what the issue is? Thanks!
Sub OpenSharepointRecentFile()
'find & open latest XYZ File
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "https://company.sharepoint.com/sites/group/Shared%20Documents/General/folder/"
MyPath = Replace(MyPath, "https:", "") 'adaptation attempt for SharePoint folder
MyPath = Replace(MyPath, "/", "\") 'adaptation attempt for SharePoint folder
MyPath = Replace(MyPath, Split(MyPath, "\")(2), Split(MyPath, "\")(2) & "@SSL")
MyFile = Dir(MyPath & "*.xlsm")
If Len(MyFile) = 0 Then
MsgBox "No Supply Headline files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
Jul 01 2022 02:21 AM