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
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Public Sub Download_All_Indices()
Dim indexName As Variant
Dim fromDate As Date, toDate As Date
Dim saveInFolder As String
saveInFolder = "F:\Temp\Excel\" 'CHANGE THIS AS REQUIRED
fromDate = InputBox("Enter From date")
toDate = InputBox("Enter To date")
For Each indexName In Array("MIDCAP", "BSE-100", "BSE-200", "BSE-500", "BSE IPO", "AUTO", "BANKEX", "CD", "CG")
Download_Daily_Historical_Data saveInFolder, CStr(indexName), fromDate, toDate
Next
End Sub
Private Sub Download_Daily_Historical_Data(saveInFolder As String, indexName As String, fromDate As Date, toDate As Date)
Dim URL As String
Dim indexValue As String
Dim fileName As String
Dim downloaded As Boolean
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
indexValue = GetIndexValue(indexName)
fileName = indexValue & Format(fromDate, "_YYYYMMDD") & Format(toDate, "_YYYYMMDD") & ".csv"
'Pad index value to 7 characters and URL-encode spaces
indexValue = Left(indexValue & String(7, " "), 7)
indexValue = Replace(indexValue, " ", "%20")
URL = "http://www.bseindia.com/stockinfo/indices_main_excel.aspx?ind=" & indexValue & _
"&fromDate=" & Format(fromDate, "MM/DD/YYYY") & "&toDate=" & Format(toDate, "MM/DD/YYYY") & "&DMY=D" '#"
downloaded = DownloadFile(URL, saveInFolder & fileName)
Debug.Print downloaded, URL
End Sub
Private Function GetIndexValue(indexName As String) As String
'Convert index name to 'indi' parameter value used in URL
Select Case UCase(indexName)
Case "SENSEX": GetIndexValue = "BSE30"
Case "MIDCAP": GetIndexValue = "MIDCAP"
Case "SMLCAP": GetIndexValue = "SMLCAP"
Case "BSE-100": GetIndexValue = "BSE100"
Case "BSE-200": GetIndexValue = "BSE200"
Case "BSE-500": GetIndexValue = "BSE500"
Case "BSE IPO": GetIndexValue = "BSEIPO"
Case "AUTO": GetIndexValue = "AUTO"
Case "BANKEX": GetIndexValue = "BANKEX"
Case "CD": GetIndexValue = "BSECD"
Case "CG": GetIndexValue = "BSECG"
Case "FMCG": GetIndexValue = "BSEFMCG"
Case "HC": GetIndexValue = "BSEHC"
Case "IT": GetIndexValue = "BSEIT"
Case "METAL": GetIndexValue = "METAL"
Case "OIL & GAS": GetIndexValue = "OILGAS"
Case "POWER": GetIndexValue = "POWER"
Case "PSU": GetIndexValue = "BSEPSU"
Case "REALTY": GetIndexValue = "REALTY"
Case "TECK": GetIndexValue = "TECK"
Case "DOL-30": GetIndexValue = "DOL30"
Case "DOL-100": GetIndexValue = "DOL100"
Case "DOL-200": GetIndexValue = "DOL200"
Case "SHARIAH 50": GetIndexValue = "SHA50"
Case Else: GetIndexValue = ""
End Select
End Function
Private Function DownloadFile(URL As String, localFilename As String) As Boolean
Dim retVal As Long
DeleteUrlCacheEntry URL
retVal = URLDownloadToFile(0, URL, localFilename, BINDF_GETNEWESTVERSION, 0)
DownloadFile = (retVal = 0)
End Function