Using Excel to submit information to a website form and parse the results Using Excel to submit information to a website form and parse the results vba vba

Using Excel to submit information to a website form and parse the results


When dealing with collections (multiple objects) I find it is best to loop through the available objects, testing each as you go.

Sub QueryInfo()    Dim ie As Object, iFRM As Long, iNPT As Long    'Application.ScreenUpdating = False   'uncomment this once it is working    Set ie = CreateObject("InternetExplorer.Application")    ie.Visible = True    ie.Navigate "website address"    'wait untli the page loads    Do While ie.busy Or ie.readyState <> 4  'READYSTATE_COMPLETE = 4        DoEvents    Loop    With ie.document.body        For iFRM = 0 To .getElementsByTagName("form").Length - 1            If LCase(.getElementsByTagName("form")(iFRM).Name) = "idinputform" Then                With .getElementsByTagName("form")(iFRM)                    For iNPT = 0 To .getElementsByTagName("input").Length - 1                        Select Case LCase(.getElementsByTagName("input")(iNPT).Name)                            Case "id_num"                                .getElementsByTagName("input")(iNPT).Value = 123                            Case "refresh_proc"                                .getElementsByTagName("input")(iNPT).Value = "menu_2"                        End Select                    Next iNPT                    .submit    '<~~ submit the form                    Do While ie.busy Or ie.readyState <> 4: DoEvents: Loop                    Exit For                End With                Exit For            End If        Next iFRM    End With    With ie.document.body        'should be at the form's destination    End With    Application.ScreenUpdating = TrueEnd Sub

There could be multiple forms on the page. Go through each one until you find the one with the correct name. Inside that form definition, are multiple input elements; cycle through each and apply parameters as necessary. When that is complete, submit the form and exit the loop.


I have had times where I couldn't for the life of me get the Form Submission to work by using the HTML elements...I found a nice little work around by figuring out where the site sets it focus and then using keystrokes to make the inputs necessary.

I admit it isn't elegant, but it got me around what was an impasse that caused me many a late night.

Application.Wait (DateAdd("S", 1, Now()))DoEvents    Call SendKeys("{TAB}", True)Application.Wait (DateAdd("S", 2, Now()))Text2Clipboard (Format(Password1, "00000000"))Application.Wait (DateAdd("S", 1, Now()))DoEvents    Call SendKeys("^v", True)Application.Wait (DateAdd("S", 1, Now()))DoEvents    Call SendKeys("{TAB}", True)Application.Wait (DateAdd("S", 2, Now()))Text2Clipboard (Password2)Application.Wait (DateAdd("S", 1, Now()))DoEvents    Call SendKeys("^v", True)Application.Wait (DateAdd("S", 1, Now()))DoEvents    Call SendKeys("~", True)Application.Wait (DateAdd("S", 2, Now()))Dim Counter As IntegerCounter = 0Do While IE.Busy    Application.Wait (DateAdd("S", 5, Now()))LoopApplication.Wait (DateAdd("S", 2, Now()))Dim links, link As ObjectSet links = IE.Document.getElementById("t-mainmenu").getElementsByTagName("a")links(1).ClickDo While IE.Busy    Application.Wait (DateAdd("S", 5, Now()))Loop