logandiana
Board Regular
- Joined
- Feb 21, 2017
- Messages
- 107
I have a sharepoint folder that will have a new excel file loaded each day.
I want to use VBA to open the most recent excel file in the sharepoint folder.
So far, if I point to the individual file, then there's no problem, works just fine.
But if I try to use Dir then I get an error on the second line
I get a run time error 52, Bad file name or number.
I've done this many times for local folders, but first time trying to pull from Sharepoint.
I am stumped!
I want to use VBA to open the most recent excel file in the sharepoint folder.
So far, if I point to the individual file, then there's no problem, works just fine.
VBA Code:
Directory = "https://companyx365-my.sharepoint.com/:x:/r/personal/personalusername_companyx_net/Documents/Documents/R%26A%20Report%20Templates/userfiles/SharepointDataFile.xlsx"
Workbooks.Open Directory
But if I try to use Dir then I get an error on the second line
Code:
Directory = "https://companyx365-my.sharepoint.com/:x:/r/personal/personalusername_companyx_net/Documents/Documents/R%26A%20Report%20Templates/userfiles/"
filename = Dir(Directory & "*.xlsx") '<<<<<< CAN'T GET PAST THIS POINT
If filename <> "" Then
MostRecentFile = filename
MostRecentDate = FileDateTime(Directory & filename)
Do While filename <> ""
If FileDateTime(Directory & filename) > MostRecentDate Then
MostRecentFile = filename
MostRecentDate = FileDateTime(Directory & filename)
End If
filename = Dir
Loop
End If
Workbooks.Open Directory & MostRecentFile
I get a run time error 52, Bad file name or number.
I've done this many times for local folders, but first time trying to pull from Sharepoint.
I am stumped!