I need to download a file from a third party web application using VBA in Excel. This is my code so far:
Dim myURL As String
myURL = "https://somewebsite/?f=13385&ver=a1df4089f0e4d11cf6b48024309fc9"
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "C:\Users\xxx\abc.xlsx", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
The trouble is that, this code saves a file successfully to the destination. But on attempting to open the file it says the file is corrupted or that the extension is incorrect. The file size however is equal to the file I obtain through a manual download.
Any help is very appreciated.