0

I need help on the following code.

I can put the data in the inputbox but it gives an error when submitting.

I think the problem comes from the "autocomplete" event

Sub VDF()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement

        IE.Visible = True
        IE.navigate "https://www.vodafone.pt/pacotes/cobertura/"
        Do While IE.Busy
        Loop
        
        Set HTMLDoc = IE.document
        
        'Alternative to Do While IE.readyState <> READYSTATE_COMPLETE
        Do While HTMLInput Is Nothing
            Set HTMLInput = HTMLDoc.getElementById("checkCoverButton")
        Loop

        'Phone
        Set HTMLInput = HTMLDoc.getElementById("phone-number")
        HTMLInput.Value = "911111111"

        'CP
        Set HTMLInput = HTMLDoc.getElementById("postalCode")
        HTMLInput.Value = Worksheet(1).Cells(4, 4)
        'image 1 the code runs so far
        
        'Submit form btn Pesquisar
        Set HTMLInput = HTMLDoc.getElementById("checkCoverButton")
        HTMLInput.Click
End Sub

The form fields are written after running the procedure but when submitting the form it gives an error as if the fields were blank

written mobile number and postal code

After Submit form btn Pesquisar

Astos
  • 1
  • 3
  • Please leave the declarations in rather than just put `Dim ...`. otherwise, people have to type it and guess what declaration types you used. Please also include the error and indicate on which line it occurs (the final click?) – QHarr Jun 06 '21 at 03:40
  • The input fields have events. You must trigger the change event after paste your text. Or the keypress event or another one, I don't know. Look here how you can do that: https://stackoverflow.com/questions/63294113/automate-ie-via-excel-to-fill-in-a-dropdown-and-continue/63299608#63299608 – Zwenn Jun 06 '21 at 11:15
  • I used the code and it doesn't work `HTMLInput.FireEvent ("onkeypress")` `HTMLInput.FireEvent ("onchange")` `HTMLInput.Value = "911111111"` [source](https://stackoverflow.com/questions/54763526/how-to-simulate-an-focus-and-typing-events) – Astos Jun 06 '21 at 16:15
  • I used the code and it doesn't work either! =( `Set event_onInput =HTMLDoc.createEvent("HTMLEvents")` `event_onInput.initEvent "input", True, False` `Set HTMLInput = HTMLDoc.getElementById("phone-number")` `HTMLInput.Value = "911111111"` `HTMLInput.dispatchEvent event_onInput` – Astos Jun 06 '21 at 16:44
  • `FireEvent` is outdated and doesn't work with most pages today. Do you had taken a look in my link? Use the sub `TriggerEvent()` from my post, look in the makro above how to work with it and look in the screenshots below how to check the events. – Zwenn Jun 07 '21 at 10:51
  • Great! Problem solved. The first time when I analyzed the code I thought it was not applicable because it is a dropdown but after your tip I decided to risk it and done. Thanks for your time @Zwenn Solution: Added `Call TriggerEvent(htmlDoc, HTMLInput, "change")` – Astos Jun 07 '21 at 17:31
  • That are good news :-) The `TriggerEvent()` sub is universal useable for all HTML events. The HTML element doesn't matter. In the macro of the linked post are also text input fields with events. – Zwenn Jun 07 '21 at 20:39
  • Yea! After seeing it better, I realized the detail and the excellent job of explaining it step by step. Congratulations on the great work! I'm new to this community. Do I need to do something to close the question or something else? – Astos Jun 09 '21 at 09:04
  • If there were an answer that solved your problem, you could mark it as a solution. But here we have only exchanged a few comments. So the thread just stays like this. It doesn't matter in this case because there is no new insight that other users would benefit from. – Zwenn Jun 09 '21 at 09:18

0 Answers0