Macro to download files from website from excel - explain
Macro to download files from website from excel - think
Excel-VBA Solutions
Sometimes our VB Applications needs to interact with websites. Downloading a file through a url is a typical example.
Here below is a code which you can use to download a file through a url. You should replace "Put your download link here" with your url. Also remember to put it inside double quotes.
Dim myURL As String myURL = "Put your download link here" Dim HttpReq As Object Set HttpReq = CreateObject("Microsoft.XMLHTTP") HttpReq.Open "GET", myURL, False, "username", "password" HttpReq.send myURL = HttpReq.responseBody If HttpReq.Status = 200 Then Set oStrm = CreateObject("ADODB.Stream") oStrm.Open oStrm.Type = 1 oStrm.Write HttpReq.responseBody oStrm.SaveToFile ThisWorkbook.Path & "\" & "file.csv", 2 ' 1 = no overwrite, 2 = overwrite oStrm.Close End If |
Also you should be careful with below line.
oStrm.SaveToFile ThisWorkbook.Path & "\" & "file.csv", 2 ' 1 = no overwrite, 2 = overwrite
In this case I have chosen the option to overwrite existing file. If you don't need to overwrite existing file please put the number accordingly.
-
-
-