0

I am using the codes provided by Solution to download sharepoint xlsx

It generates a file on my destination folder, but I am not able to use it. It seems to be corrupt.

Do I link the file to download wrongly?

Or is the issue an xlsm file for which the code was not designed?


Option Explicit
#If VBA7 Then
    Private Declare PtrSafe 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
#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 DownloadFileFromWeb(strURL As String, strSavePath As String) As Long

        ' strSavePath includes filename
        DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
    End Function


Sub GetFile()
    Dim urlName  As String
    urlName = "https://talanx.sharepoint.com/:x:/r/sites/LiabilityGlobalRisk/_layouts/15/Doc.aspx?sourcedoc=%7BC39E3D22-D263-4519-B6A5-33B64496F6E0%7D&file=Test.xlsm&action=default&mobileredirect=true"
    Dim fName As String
    fName = "J:\TEST\Test.xlsm"
    URLDownloadToFile 0, urlName, fName, 0, 0

End Sub

Florian
  • 31
  • 5
  • Web downloaded files are usually locked. Did you check that? (right click/properties) – Kostas K. Jun 17 '22 at 09:08
  • If the file being "blocked" is the problem, and your next question is how to unblock it using VBA, you cannot, as that is protection from malicious files that people download. – braX Jun 17 '22 at 09:13
  • Open the file with a text editor and see if it contains HTML and is not actually an Excel file. The easiest way to fetch a file from SharePoint is to open it in excel and then save it to disk from there. – Tim Williams Jun 17 '22 at 16:03

0 Answers0