Home » excel » html – Download file from webpage using Excel VBA

html – Download file from webpage using Excel VBA

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to download rain gauge data from a website with VBA.

I found the rain gauge station number defined by user input. After the search is done, my code to select the checkbox corresponding to the rain gauge station and the data format isn’t working.

When I do this manually, after the search is done, I have to click on “Dados Convencionais” to show the result of the search. I couldn’t find a way to do it in code.

Sub DownloadCSV()

Dim SearchString As String
Dim SearchBox As Object
Dim SearchButton As Object
Dim SelectionStationButton As Object
Dim SelectionCSVButton As Object
Dim DownloadButton As Object
Dim ie As New InternetExplorer

'User inputs station number
SearchString = InputBox("Input rain gauge station number", "Download data HidroWeb", "Station number- e.g. 02044054")

With ie
    .Visible = True
    .Navigate "http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"

    While ie.ReadyState <> 4
    DoEvents
    Wend

    'Station number to be searched
    Set SearchBox = .Document.getElementById("form:fsListaEstacoes:codigoEstacao")
    SearchBox.Value = SearchString

    'Search button click
    Set SearchButton = .Document.getElementById("form:fsListaEstacoes:bt")
    SearchButton.Click

    'select checkbox beside the station number
    Set SelectionStationButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:j_idt178:table:0:ckbSelecionada")
    SelectionStationButton.Click

    'Select data format -  Arquivo Excel(.CSV)
    Set SelectionCSVButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:radTipoArquivo:2")
    SelectionCSVButton.Click

    'click download button
    Set DownloadButton = .Document.getElementById("form:fsListaEstacoes:fsListaEstacoesC:btBaixar")
    DownloadButton.Click

End With

End Sub
How to&Answers:

I have tried to keep close to your original code. Here are the steps which include a missing step of making the dropdown appear so you can select format etc.

Option Explicit

Sub DownloadCSV()

    Dim SearchString As String
    Dim SearchBox As Object
    Dim SearchButton As Object
    Dim SelectionStationButton As Object
    Dim SelectionCSVButton As Object
    Dim DownloadButton As Object
    Dim ie As New InternetExplorer

    'User inputs station number
    SearchString = "02044054"                    'InputBox("Input rain gauge station number", "Download data HidroWeb", "Station number- e.g. 02044054")

    With ie
        .Visible = True
        .Navigate2 "http://www.snirh.gov.br/hidroweb/publico/medicoes_historicas_abas.jsf"

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

        'Station number to be searched
        Set SearchBox = .document.getElementById("form:fsListaEstacoes:codigoEstacao")
        SearchBox.Value = SearchString

        'Search button click
        Set SearchButton = .document.getElementById("form:fsListaEstacoes:bt")
        SearchButton.Click

        'click dropdown
        .document.querySelector("[href*=dadosConvencionais]").Click

        'select checkbox beside the station number
        .document.querySelector(".checkbox.i-checks i").Click

        'Select data format -  Arquivo Excel(.CSV)
        .document.querySelector("input[value='3']").Click

        'click download button
        .document.querySelector("[id='form:fsListaEstacoes:fsListaEstacoesC:btBaixar']").Click

        Application.Wait Now + TimeSerial(0, 0, 10)
        .Quit
    End With

End Sub