Use VBA to press Send button on Webpage - what am I doing wrong? RRS feed

  • Question

  • Can anybody give me a hand --- 

    I want to press the Send button on a Webpage using VBA code  -- 

    I can see exactly where I need to be in the html code -- but i cannot get a getElementbyClassName or any other method to to work 

    here is the code (copied from another discussion in this group)


    Public Sub Press_Button()
    Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlButton As Object    'MSHTML.HTMLButtonElement
    Dim htmlColl As MSHTML.IHTMLElementCollection

        sUrl_Login = ""
        sURL_FormGuide = ""

        Set objIE = New SHDocVw.InternetExplorer

        With objIE
            .navigate sURL_FormGuide ' Main page
            .Visible = True
            Do While .READYSTATE <> 4: DoEvents: Loop
            Application.Wait (Now + TimeValue("0:00:02"))
                 'set user name and password
            Set htmlDoc = .document
            Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
            Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop

            For Each htmlInput In htmlColl
                If htmlInput.Name = "account_id" Then
                    htmlInput.Value = "xxxxx"
                    If htmlInput.Name = "account_password" Then
                        htmlInput.Value = "xxxxx"
                    End If
                End If
            Next htmlInput

             'click login
            Set htmlDoc = .document
            Set htmlColl = htmlDoc.getElementsByTagName("input")
            Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop

            For Each htmlInput In htmlColl
                If Trim(htmlInput.Type) = "submit" Then

                    Exit For
                End If
            Next htmlInput

            Set htmlButton = htmlDoc.getElementsByClassName("different-button")     'race-resultss-wrapper ng-scope")
            Do While htmlDoc.READYSTATE <> "complete": DoEvents: Loop
            For Each htmlInput In htmlColl
                Debug.Print htmlInput.Type
                'If Trim(htmlInput.Type) = "submit" Then
                '    htmlInput.Click
                '    Exit For
                'End If
            Next htmlInput       
        End With
    End Sub

    Any help will be greatly appreciated -- 

    Tuesday, July 4, 2017 1:02 PM

All replies

  • The webpage is only available in Australia.  Hard to help when we can't access it.  I have had numerous problems with Microsoft Internet Controls.  I would recommend using Selenium.  Someone wrote a .com addin that uses Selenium and can be called from VBA, SeleniumBasic.  The release is dated but I've managed to get it working with IE, Chrome, Edge, FF (v46.0.1). 
    Tuesday, July 4, 2017 2:58 PM
  • Thanks for the reply 

    I made a mistake with the upload --- hopefully this works better .. 

    Tuesday, July 4, 2017 3:28 PM
  • finally - I think I got it right -- SORRY

    Tuesday, July 4, 2017 3:32 PM
  • That code uses READYSTATE and DoEvents and Application.Wait. It is not good code to copy. See my Introduction to Web Site Scraping. It could be better but at least it shows use of the DocumentComplete event. The DocumentComplete event should be used instead of READYSTATE to determine when the document can be used.

    The code in that article was originally VBA code in an Access macro, many years ago.

    Sam Hobbs

    Tuesday, July 4, 2017 6:21 PM