Using VBA to download and save a file which behind a login (but do not want the code to perform login)

wmc

New Member
Joined
Jan 2, 2012
Messages
22
Hi there.

I have a number of files that need to be downloaded periodically and require me to log into a secure website in order to do so. I would like to automate the download, but not the login. I want to log in manually, and then kick off the macro.

I have searched the this forum and the web in general. I found a number of solutions that would appear to work, but none of them seems to do the trick. The URL to initiate the download is saved in cell J8. The filename I would like to use is saved in cell B8. How can I use Excel to automatically download and save the file, overwriting the existing file if one exists?

Here is the code that seems to be most promising:

Code:
Sub DownloadFile()

Dim website As String
Dim filename As String
website = Range("J8").Value
filename = Range("B8").Value

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", website, False
WinHttpReq.send

OpenX = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.write WinHttpReq.responseBody
    oStream.SaveToFile ("\\network folder\my target folder\" & filename & ".xls")
    oStream.Close
End If

End Sub

I get Run-time error '-2147467259 (80004005)':
Method 'open' of object 'IXMLHTTPRequest' failed on this line:
Code:
WinHttpReq.Open "GET", OpenX, False

Any ideas?

Thanks in advance for any assistance!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top