Sub TestDown()
DownloadFile "url", "folderpath"
End Sub
Function DownloadFile(myURL As String, saveToPath As String)
Dim WinHttpReq As Object
Dim iTimer As Long
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
Debug.Print WinHttpReq.readyState
WinHttpReq.Send
'make sure readystate is finished
iTimer = Timer
Do While WinHttpReq.readyState = 1
'if 10 seconds elapse and nothing happens, abort:
If Timer - iTimer > 10 Then Exit Do
Loop
'readystate 4 = all data received
If WinHttpReq.readyState = 4 Then
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.ResponseBody
oStream.SaveToFile saveToPath, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
MsgBox ("Download Complete")
Else
MsgBox ("HTTP error: " & WinHttpReq.Status)
End If
Else
MsgBox ("Couldn't get file")
End If
Set oStream = Nothing
Set WinHttpReq = Nothing
End Function