Download a file from URL from inside Excel into certain path, returns blank if download is successful, or error number if failed
Function DownloadFile(sURL, sFilePath)
' Dim myURL As String
' myURL = "https://YourWebSite.com/?your_query_parameters"
'
On Error Resume Next
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", sURL, False ', "username", "password"
WinHttpReq.send
If Err.Number > 0 Then
DownloadFile = Err.Number
Exit Function
End If
rURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile sFilePath, 2 '"C:\file.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
DownloadFile = ""
End Function
' Dim myURL As String
' myURL = "https://YourWebSite.com/?your_query_parameters"
'
On Error Resume Next
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", sURL, False ', "username", "password"
WinHttpReq.send
If Err.Number > 0 Then
DownloadFile = Err.Number
Exit Function
End If
rURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile sFilePath, 2 '"C:\file.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
DownloadFile = ""
End Function
sURL, sFilePath
Views 3,254
Downloads 1,329
CodeID
DB ID
ANmarAmdeen
609
Revisions
v1.0
Tuesday
May
22
2018