1
votes

I am using the following code to download a .xlsx file from web on Excel VBA.

Sub Download()
        Const MYURL = "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx"
        Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
        objHTTP.Open "GET", MYURL, False


    objHTTP.Send

    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write objHTTP.ResponseBody
    oStream.SaveToFile ("C:\wamp\file.xlsx")
    oStream.Close
End Sub

The code succeeds to download the file and saves it in the directory. But instead of saving it I wish I could open the xlsx file on Excel. Is it possible?

2

2 Answers

1
votes
Set wb = WorkBooks.open( _
  "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx")
0
votes

So after some research instead of trying to opening the file directly from the memory I just saved it on the user's default temp path and then opened it. Here is the code:

Sub Download()
    Const MYURL = "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx"
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    objHTTP.Open "GET", MYURL, False

    objHTTP.Send

    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write objHTTP.ResponseBody
    oStream.SaveToFile (Environ("TEMP") & ".xlsx")
    oStream.Close

    Workbooks.Open Environ("TEMP") & ".xlsx"

End Sub