0

I have an Excel file which is shared via Onedrive link. I need to download this file to PC (c:\TEST).

I found this code, it downloaded the file, but the file is corrupted or something.

Sub DownloadFile()
    MsgBox ("12")
    Dim myURL As String
    myURL = "https://foxhunter1-my.sharepoint.com/:x:/g/personal/name_example/Eexy0m7o08hBlRJAP_xh64wBN45j70JIw2E-CDlJGgZILg?e=cbs4iF"

    Dim WinHttpReq As Object
    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    With WinHttpReq
        WinHttpReq.Open "GET", myURL, False, "contoso\user", "password"
        WinHttpReq.send
    
        If WinHttpReq.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            oStream.Open
            oStream.Type = 1
            oStream.Write WinHttpReq.responseBody
            oStream.SaveToFile "C:\TEST\TEST.xlsx", 2 ' 1 = no overwrite, 2 = overwrite
            oStream.Close
        End If
    End With
End Sub

ERROR: error after download

Community
  • 1
  • 1
Andrew
  • 25
  • 7
  • 1
    Are youe sure the link is correct and if it is are you sure that it is really an _XLSX_ file? – Storax Oct 14 '20 at 12:43
  • See this answer: https://stackoverflow.com/questions/42419486/how-to-download-a-file-from-sharepoint-with-vba/42422964#42422964 – Tragamor Oct 14 '20 at 12:51
  • Link is correct, you can open it in every browser, and it is .xlsx file. – Andrew Oct 14 '20 at 12:53
  • 1
    After the discussion below my answer I am pretty sure the link is not the correct one for downloading the file. It is just the link to open the file in a browser. You need to get the direct link to the file which should also contain the Excel filename which ends with xlsx. – Storax Oct 15 '20 at 08:02

1 Answers1

1

Try the following code (I got it very likely from here as stated in Tragamor's comment)

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

Then you only need to

Sub GetFile()
    Dim urlName  As String
    urlName = "your Url"
    Dim fName As String
    fName = "C:\TEST\test.xlsx"
    URLDownloadToFile 0, urlName, fName, 0 , 0

End Sub
Storax
  • 11,158
  • 3
  • 16
  • 33
  • 1
    Please link resource if you use it. This is from here: https://stackoverflow.com/questions/42419486/how-to-download-a-file-from-sharepoint-with-vba/42422964#42422964 – Tragamor Oct 14 '20 at 12:51
  • When I copy pase your code, the part from "Privete Declare... to ... ) As Long" is red colored. An if I run Sub GetFile() with my URL, I get compile error: The code in this project must be updated for use on 64-bit system. Please review and update Declare statements and then mark them with the PtrSafe attribete. – Andrew Oct 14 '20 at 12:59
  • Ok, give me a sec to adjust the code for a 64-bit Excel BUT if you know how to do it why don't you do it? – Storax Oct 14 '20 at 13:02
  • @Tragamor: You are probably right that I got it there but I copied it from my code library. And that's how I lost track :-( – Storax Oct 14 '20 at 13:09
  • To be honest I'm still beginner in VBA and self teacher. Im not use VBA on daily basis, only when I need automate something. Anyway thank you for your time and help. If you visit our store www.skinzone.eu I can thank you with our products :) – Andrew Oct 14 '20 at 13:10
  • @Andrew: Changed the answer. – Storax Oct 14 '20 at 13:14
  • @Tragamor: You must be right because I already upvoted your answer in the past. – Storax Oct 14 '20 at 13:21
  • @Storax Sub GetFile() gives me Compile error Argument not optional over (URLDownloadToFile). – Andrew Oct 14 '20 at 13:34
  • Sorry, forgot to add the zeros at the end, changed that. – Storax Oct 14 '20 at 17:42
  • @Storax Thank you mate, file is downloading, but now we are at the beginning. File was successfully downloaded to folder, but is corrupted :( Excel canot open the file test.xlsx..... – Andrew Oct 15 '20 at 06:24
  • I've tried different cloud service (Gdrive, Mega.nz) same result. Then I've tried change the file to simple txt file, and it is corrupted too. Where the bug can be? :( – Andrew Oct 15 '20 at 06:56
  • Then I guess the download is not the problem. There seems to be a problem with the file. Maybe you try to upload a working xlsx file to the sharepoint site, download it and open it again. If that's is working then you have a problem with the file in question. – Storax Oct 15 '20 at 07:09
  • Im not the only one who have this problem [link](https://stackoverflow.com/questions/33565291/excel-file-corrupted-on-download-to-vba-app-from-net-website) Im not fully understanding the solution :( – Andrew Oct 15 '20 at 07:14
  • Maybe it is a simmilar problem but I am not sure. I used the above code to download several xlsx files from a sharepoint site and it is working fine for me. So, at this point, I am afraid I don't have any further tipps to help. – Storax Oct 15 '20 at 07:31
  • I understand, can you show me an example of URL what you using? – Andrew Oct 15 '20 at 07:34
  • No, sorry, that is an internal URL. – Storax Oct 15 '20 at 07:35
  • What I can say, it is slighlty different and it also contains the file name of the xlsx file like _myfile.xlsx_ and it is like _https://abc.sharepoint.com/:x:/dir1/dir2/dir3/filename.xlsx_ So it's different to your URL, maybe you got the wrong URL. – Storax Oct 15 '20 at 07:41
  • I am prrety sure you got the wrong link. Change the extension of your downloaded file to _html_ and open it. – Storax Oct 15 '20 at 07:52
  • Changing extension to .html opens file in browser and it is look ok. – Andrew Oct 15 '20 at 08:19
  • 1
    So, you have not gotten the direct link to the xlsx file. You need to get the correct link first and then you can download the file. For example you open the file directly in Excel on sharepoint, open VBE an retrieve the full name with `ThisWorkbook.Fullname`. – Storax Oct 15 '20 at 08:31