Difficulty in Downloading files

zaska

Well-known Member
Joined
Oct 24, 2010
Messages
1,046
Hello,

I download few files daily from the following links and i would like to seek the help of members of this forum in automating the same using VBA to download files daily ( specifying from date & Todate ) to E drive


http://www.nseindia.com/content/historical/EQUITIES/2011/JUL/cm18JUL2011bhav.csv.zip

http://www.nseindia.com/content/historical/DERIVATIVES/2011/JUL/fo18JUL2011bhav.csv.zip

http://www.nseindia.com/archives/equities/mto/MTO_18072011.DAT

http://www.bseindia.com/BSEDATA/gross/2011/SCBSEALL1807.zip

http://www.bseindia.com/BSEDATA/gross/2011/SCBSEALL1807.zip

Any help would save me considerable amount of time daily.


Thank you
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
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


Private Sub DownloadFile(URL As String, TargetFile As String)
    If URLDownloadToFile(0, URL, TargetFile, 0, 0) <> 0 Then
        MsgBox "Error occured.", vbCritical
    Else
        MsgBox "File has been downloaded!", vbInformation
    End If
End Sub
 
Upvote 0
URL = "http://www.nseindia.com/content/historical/EQUITIES/2011/JUL/cm18JUL2011bhav.csv.zip"
TargetFile = "C:\cm18JUL2011bhav.csv.zip"
 
Upvote 0
Sir,

I added the same but the code is not running and asking me the macro name.

Moreover the date in the URL links changes daily. How to handle this?


Code:
[B]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
Private Sub DownloadFile(URL As String, TargetFile As String)
    URL = "http://www.nseindia.com/content/historical/EQUITIES/2011/JUL/cm18JUL2011bhav.csv.zip"
    TargetFile = "C:\cm18JUL2011bhav.csv.zip"
    If URLDownloadToFile(0, URL, TargetFile, 0, 0) <> 0 Then
        MsgBox "Error occured.", vbCritical
    Else
        MsgBox "File has been downloaded!", vbInformation
    End If
    
End Sub
[/B]

Thank u
 
Upvote 0
Sir,

I tried the following code which worked. But how to make this work for all the five URL's posted above at once and also how to loop the code for downloading files of more days.

Option Explicit

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

Private Sub Example()
'From All API
DownloadFile _
"http://www.nseindia.com/content/historical/EQUITIES/2011/JUL/cm18JUL2011bhav.csv.zip", _
"E:\cm18JUL2011bhav.csv.zip"
End Sub

Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function

Thank u
 
Upvote 0
You will need to put the URL addresses in an excel sheet.
E.g. Sheet1, Column A (URL), Column B (SavePath)
Then edit the code to loop through the list like:
Code:
Option Explicit
Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Private Sub LoopThroughURLs()
'From All API
'example by Matthew Gates ([EMAIL="Puff0rz@hotmail.com"]Puff0rz@hotmail.com[/EMAIL])
'http://www.vbaexpress.com/forum/showthread.php?t=29333
Dim lLR As Long
Dim r As Range
lLR = Range("A" & Rows.Count).End(xlUp).Row
If lLR = 1 And Range("A1").Value = "" Then Exit Sub
For Each r In Range("A1:A" & lLR)
DownloadFile r.Value, r.Offset(, 1).Value
Next r
End Sub
Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
And do not remove the VBA comments. They will be useful when your mind goes blank and you don't remember where did you pick them up!
 
Upvote 0
Sir,

The above code is not running and asking for a Macro name and moreover the URL is dynamic ..the date contained in the URL's changes everyday.

How do i handle this? I don't have Good knowledge about VBA.


Thank u
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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