macro to download multiple files from websites.

abhay_547

Board Regular
Joined
Sep 12, 2009
Messages
179
Hi All,

I have the below mentioned code which downloads the file from a website.
but the issue is that it doesn't directly download the file on the given path it gives a popup box to get the confirmation from the user that he wants to "Open", "Save" or "Cancel" the download. I want that the user shouldn't get any such popup and the code should directly download the file to the given path apart from this I want to use the same code to download multiple files at one go.

The below code works fine for one link but if I try to download the files from multiple links it doesn't work.

Here is my code :

[vba]
Sub Downloadfilesfromnet()
'Need to reference to Microsoft Internet Controls
Dim URL As String
Set IE = CreateObject("internetexplorer.application")
URL = "http://www.xyz.com/abc.zip" 'for TEST
Dim IE As Object
IE.Visible = True
IE.Navigate URL
Do While IE.ReadyState <> 4
DoEvents
Loop
SendKeys "%S"
IE.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT 'SaveAs
End Sub
[/vba]



Thanks a for your help in advance.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi John,

Here is the code which I got when I had done a search using the "URLDownloadToFile". But how to use this code for downloading multiple files at one go.

HTML:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
  "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
    szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
  
Sub DownloadFilefromWeb()
    Dim strSavePath As String
    Dim URL As String, ext As String
    Dim buf, ret As Long
    URL = Worksheets("References & Resources").Range("URLMSL").Value
    buf = Split(URL, ".")
    ext = buf(UBound(buf))
    strSavePath = ThisWorkbook.Path & "\" & "DownloadedFile." & ext
    ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
    If ret = 0 Then
        MsgBox "Download has been succeed!"
    Else
        MsgBox "Error"
    End If
End Sub

Thanks a lot for your help advance. :)
 
Upvote 0
URLDownloadToFile downloads one file at a time. For multiple simultaneous downloads, you could try Download files asynchronously using URLMON.DLL from http://www.mvps.org/emorcillo/en/code/vb6/index.shtml. This implements an asynchronous interface to URLDownloadToFile allowing you to download multiple files at the same time. It's written for VB6 but should also work in VBA. You'll also need OLELIB.TLB at the top of the page.
 
Upvote 0
Hi John,

As suggested by you in your earlier post I have downloaded the URLMON.DLL file from the link provided by you. But I am not sure about OLELIB.TLB. I would appreciate if you can provide me an working example of what you have suggested. Please....


Thanks a lot for your help in advance. :)
 
Upvote 0
I had a look at the adl.zip download and it seems the description of 'asynchronous' is a bit misleading. The Download method in the AsyncDownload class is not asynchronous because it doesn't return immediately to the caller. It returns only when the download is completed or aborted. It just provides callbacks as to the progress of the download. The result is that it can only download one file at a time, so no good for your requirement.
 
Upvote 0
Hi John,

Thanks a lot for your quick reply. Can you please let me any other option then.


Thanks a lot for your help in advance. :)
 
Upvote 0
Hi All,

Did anyone get the chance to look into the above thread..

Thanks a lot for your help in advance..:)
 
Upvote 0
Hi All,

Did anyone get the chance to look into the above thread..

Thanks a lot for your help in advance..:)


The code below worked well for one file, but how would I download multiple files?

Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long


Sub DownloadFileFromWeb()
Dim i As Integer

Const strUrl As String = "http://teams/Dept/Shared Documents/Reports/Pivot_Source_Data/filename.csv"
Dim strSavePath As String
Dim returnValue As Long

strSavePath = "C:\Reports\Pivot_Source_Data\xxxxxxxx.CSV"
returnValue = URLDownloadToFile(0, strUrl, strSavePath, 0, 0)

End Sub</SPAN></SPAN>
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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