Ashish
Ashish

Reputation: 83

Not able to click on submit button using vba

When i go to fatch data using (from date) to (to date), click on show button using vba not working

Also i need suggestion, How can i download csv file from multiple dates

i wanted to download csv file for each date between date range

Dim MP, FD, TD As String
MP = ActiveWorkbook.Name
FD = Sheets("Sheet1").Range("XFA2")
TD = Sheets("Sheet1").Range("XFB2")
Dim IE As New SHDocVw.internetexplorer
Dim HTMLDoc As MSHTML.HTMLDocument
IE.Visible = True
IE.navigate "https://www.mcxindia.com/market-data/bhavcopy"
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
For N = FD To TD
Workbooks(MP).Worksheets("Sheet1").Range("XEX2").Value = (FD)
N1 = Workbooks(MP).Worksheets("Sheet1").Range("XEX3").Value
HTMLDoc.getElementById("txtDate").Value = (N1)
HTMLDoc.getElementById("btnShowDatewise").Click
Next N

Suggest me the codes to click on submit button and how to download multiple date files,since show button takes time to publish data and then i could click on csv tab on rightside penal of webpage

Upvotes: 0

Views: 83

Answers (1)

QHarr
QHarr

Reputation: 84455

If you are only setting the one day you can do the following which includes a check that the date produces a download file

Option Explicit   
Public Sub Download()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.mcxindia.com/market-data/bhavcopy"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .parentWindow.execScript "document.querySelector('#cph_InnerContainerRight_C001_txtDate_hid_val').value='20190201';"
            .parentWindow.execScript "document.querySelector('#txtDate').value='20190201';"
            .querySelector("#btnShowDatewise").Click

            If .querySelectorAll("#cph_InnerContainerRight_C001_lnkExpToCSV").Length > 0 Then
                .parentWindow.execScript "__doPostBack('ctl00$cph_InnerContainerRight$C001$lnkExpToCSV','');"
            End If

        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
        '.Quit
    End With
End Sub

Upvotes: 1

Related Questions