Code is working properly, but when I try to open file it is corrupt. I cannot open it, but when I do open it in notepad I have several lines of html code, so it seems like it is not downloading file exactly but some other sharepoint webpage components or other stuff.
Option Explicit
#If Win64 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" ( _
ByVal pCaller As LongLong, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongLong, _
ByVal lpfnCB As LongLong) As LongLong
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
#End If
Function DownloadFile(Url As String, SavePathName As String) As Boolean
DownloadFile = URLDownloadToFile(0, Replace(Url, "\", "/"), SavePathName, 0, 0) = 0
End Function
Sub Demo()
Dim strUrl As String, strSavePath As String, strFile As String
strUrl = "https://zzzzzzz.ll.xlsx"
'SharePoint Path for the file
strSavePath = "C:\Users\username\Desktop\" 'Folder to save the file
strFile = "CompanySalesReport" & ".xlsx"
If DownloadFile(strUrl, strSavePath & strFile) Then
MsgBox "File saved to: " & vbNewLine & strSavePath
Else
MsgBox "Unable to downloaf file:" & vbNewLine & strFile & vbNewLine & "Check url string and that document is shared", vbCritical
End If
End Sub