vba getting latest file from sharepoint

dwpang

New Member
Joined
Jun 21, 2021
Messages
1
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
hi, i am trying to retrieve the latest file from sharepoint folder every week via vba.

1624294878931.png


so, the files are named as XXX XXX XXX 20210614.xlsx as you see in the picture.
i got the url by opening one of them and copied the link to input as the url in the vba.

mypath = "https://xxx.sharepoint.com/sites/xxx-xxx-xxx-xxx/Shared Documents/xxx xxx/Weekly reports/"
mypath = Replace(mypath, "/", "\")
mypath = Replace(mypath, "https:", "")

myfile = Dir(mypath & "*.xlsx", vbNormal)
If Len(myfile) = 0 Then
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
Set wb = Workbooks.Open(mypath & latestfile)

problem is whenever i come to --- myfile = Dir(mypath & "*.xlsx", vbNormal)
i will have a run-time error 52: bad file name or number

any experts or advice?
p.s. mapping to drive is not an option for me currently
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Assuming that you have access privileges and the path is correct, it should work. Also, make sure that the number of characters in the path does not exceed the 260 character limit (see Naming Files, Paths, and Namespaces - Win32 apps). And, lastly, check the file itself to see whether the filename contains Unicode characters. If so, use the FileSystemObject object instead, which supports Unicode.
 
Upvote 0

Forum statistics

Threads
1,215,741
Messages
6,126,587
Members
449,319
Latest member
iaincmac

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top