Issue
I have created a report via Amazon API. API returns url. Here is an instruction how to get the file
src="https://i.stack.imgur.com/Sy87k.png" alt="enter image description here" />
I tried the following code
Sub t()
TargetURL = "https://offline-report-storage-eu-west-1-prod.......d"
'Set HTTPReq = CreateObject("MSXML2.XMLHTTP")
'Set HTTPReq = CreateObject("MSXML2.ServerXMLHTTP")
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTPReq.Option(4) = 13056 '
HTTPReq.Open "GET", TargetURL, False
HTTPReq.send
End Sub
Look like the request is ok - State=200
But the file was not loaded.
When I copied the link to a browser I got the file. So, the link was correct.
How can I load the file without referring/opening the browser?
Solution
You are almost there, just add a few more lines of code. I would use the ADODB.Stream to save the response a file:
' Is reqeust successful?
If HTTPReq.Status = 200 Then
Set FileStream = CreateObject("ADODB.Stream")
FileStream.Open
FileStream.Type = 1 ' Binary stream
FileStream.Write HTTPReq.responseBody
FileStream.Position = 0
FileStream.SaveToFile LocalFileName, 2 ' Overwrite file exists
FileStream.Close
Else
MsgBox "Error: " & HTTPReq.Status & " " & HTTPReq.statusText
End If
Answered By - vbakim Answer Checked By - David Marino (WPSolving Volunteer)