How to update a website page after changing the value of a field by VBA

Asked

Viewed 279 times

1

Good night.

I wonder how I can update the page with the date I informed by VBA. I put the date but the page does not change. Since there’s no button, I don’t know what to do.

Sub Links()

    Dim ie              As Object
    Dim tabela          As Object
    Dim Liga, DataHora  As String
    Dim Home, Away      As String
    Dim Link, data      As String
    Dim ResultadoFinal  As String
    Dim Posiz1, Posiz2  As Long
    Dim linha

    data = ThisWorkbook.Sheets("Plan1").Range("AD2")

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    URL = "https://betsapi.com/cs/basketball/"
    ie.Navigate URL

    State = 0
    Do Until State = 4
        DoEvents
        State = ie.ReadyState
    Loop

    'até consigo mudar a data, mas ao mudar a data a pagina deveria se ajustar a essa data 
    '(como ocorre ao clicar de forma manual), mas isso não ocorre ao preencher pelo vba.
    ie.Document.forms.Item(1).Item(0).Value = Sheets("Plan1").Range("AD2") 'conte uma data. Ex: 2018-05-06

    Text1 = ie.Document.Body.innerHTML

    Text1 = Replace(Text1, "a href=/r/", "CJOS QUER ")


    'Coleta a parte importante dos links com base no meu marcador
    Posiz1 = 1
    i = 1
        Do
            On Error GoTo sai

            Posiz1 = InStr(Posiz1, Text1, "CJOS QUER ")

            Posiz1 = InStr(Posiz1, Text1, "CJOS QUER ") + 10
            Posiz2 = InStr(Posiz1, Text1, ">")

            Link = Trim(Mid(Text1, Posiz1, Posiz2 - Posiz1))
            c = InStr(1, Link, "/")
            Range("A" & i) = Left(Link, c - 1)
            Range("H" & i) = "https://betsapi.com/rs/bet365/" & Link
            Posiz1 = Posiz2 + 1
            i = i + 1
        Loop
sai:

    ie.Quit
    Set tabela = Nothing
    Set ie = Nothing

End Sub
  • Declaring the variable Dim formData As MSHTML.HTMLFormElement, defining how Set formData = ie.document.getElementById("datepicker").form and sending the form formData.submit it is possible to send. However it does not send the date changed with the value .Value, but today’s date. I used Fireevent "Onchange" and keybd_event VK_RETURN, 0&, 0&, 0& without success. Let’s wait to see if anyone has the answer and I’ll ask on the Soen to see if I can get the answer because I was curious.

1 answer

1


Look at the solution I found. It’s not elegant, but it’s working (I don’t know how long)... If anyone has a better idea, I accept.

Sub Links()

    Dim ie              As Object
    Dim tabela          As Object
    Dim Liga, DataHora  As String
    Dim Home, Away      As String
    Dim Link, data      As String
    Dim ResultadoFinal  As String
    Dim Posiz1, Posiz2  As Long
    Dim linha

    data = ThisWorkbook.Sheets("Plan1").Range("AD2")

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    'A data tem uma / no final. Ex: 2018-05-06/
    URL = "https://betsapi.com/cs/basketball/" & data
    ie.Navigate URL

    State = 0
    Do Until State = 4
        DoEvents
        State = ie.ReadyState
    Loop

    ie.Refresh
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5)

    Text1 = ie.Document.Body.innerHTML

    Text1 = Replace(Text1, "a href=/r/", "CJOS QUER ")


    'Coleta a parte importante dos links com base no meu marcador
    Posiz1 = 1
    i = 1
        Do
            On Error GoTo sai

            Posiz1 = InStr(Posiz1, Text1, "CJOS QUER ")

            Posiz1 = InStr(Posiz1, Text1, "CJOS QUER ") + 10
            Posiz2 = InStr(Posiz1, Text1, ">")

            Link = Trim(Mid(Text1, Posiz1, Posiz2 - Posiz1))
            c = InStr(1, Link, "/")
            Range("A" & i) = Left(Link, c - 1)
            Range("H" & i) = "https://betsapi.com/rs/bet365/" & Link
            Posiz1 = Posiz2 + 1
            i = i + 1
        Loop

    sai:
    ie.Quit
    Set tabela = Nothing
    Set ie = Nothing

End Sub
  • Wow, truth. I didn’t even realize that changing the href could be accomplished.

  • Worse than I had ever tried it, but when updating the page it went to today (updating manually)... But with ie.Refresh she updated keeping the chosen date

  • Just an addendum to your code... FWIW: Your statement is wrong, because with Dim Liga, DataHora As String, Alloy is declared as Variant and Date Time as String. Correct would be Dim Liga As String, DataHora As String

  • I didn’t understand... I declared: Dim Liga, Datahora As String Wouldn’t be the two variables being String?

  • Not in VBA, League is as Variant. After the declaration see Debug.Print TypeName(Liga) and Debug.Print TypeName(DataHora). One will be string and another Empty that is Variant. At first I also declared wrong because of another programming language I learned.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.