0

I'm trying to check all the checkboxes available on a webpage via VBA since the name convention doesnt appear to be one in which I can be selective. However I cannot seem to get anything to work. I can login to the website and navigate to the section of the website I want but cannot cross this hurdle. Any help would be greatly appreciate. Below is the source code from the webpage.

       <li data-product-family="30yr"
            data-product-amortizationTerm="30"
            data-product-type="Conventional"
            data-product-amortizationType="Fixed"

        >
        <label>
        <input type="checkbox" 
            value="154232" 
            class="product-Conventional product-item" 
            data-authorized-remittance-types="ActualActual "
            />30-Year Fixed Rate - 110k Max Loan Amount</label>
        </li>

VBA I attempted to write (edited)... code I'm using presently:

Public Sub TestIE()
Dim IE As Object
Dim aNodeList As Object, i As Long


' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

' You can uncoment Next line To see form results
IE.Visible = False

' Send the form data To URL As POST binary request
IE.Navigate "https://"

' Statusbar
Application.StatusBar = "Page is loading. Please wait..."

' Wait while IE loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

IE.Visible = True

Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length
aNodeList.Item(i).Checked = True
Next i 
End Sub
6
  • Your Exit For instructs the loop to exit upon the very first match. Commented Jul 12, 2018 at 14:38
  • Thanks Bill, I'm somewhat new to this. I would expect that if that was the only issue it would at least check the first checkbox, which it doesnt appear to do. I get an automation error and unspecified error when running the code. Commented Jul 12, 2018 at 14:52
  • I cannot seem to get anything to work or but cannot cross this hurdle: these statements don't give us a lot to work on. Are you getting an error message? what happens when you run the code? Plus, don't use a Click, you should be able to use .Checked property to set the checkbox.. unless you are working with embedded elements? Commented Jul 12, 2018 at 14:53
  • See errors above. I tried changing to 'checked' from 'Click' to no avail. Exact error reads: Run-time error '-2147467259 (80004005)': Automation error Unspecified error Commented Jul 12, 2018 at 14:57
  • Just to clarify, to set the element as checked, you would set it as: objElement(i).Checked = True Commented Jul 12, 2018 at 15:29

1 Answer 1

1

You can try to get a nodeList of the checkboxes with:

IE.document.querySelectorAll("input[type=checkbox]")

You can traverse the nodeList along its .Length property.

E.g.

Dim aNodeList As Object, i As Long
Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length -1
    On Error Resume Next
    aNodeList.item(i).Checked  = True
    On Error GoTo 0
Next i
Sign up to request clarification or add additional context in comments.

4 Comments

QHarr, that works great. That said, it did create a new error: Run time error 424 Object Required. Any thoughts? Not sure if its trying to continuous loop? I updated the original post with the code I'm now using.
I suspect you might have to set for condition as: For i = 0 To aNodeList.Length - 1 maybe?
Damn did I miss that. Sorry. Thanks zac! Ye, it is zero based so you go to length -1. You can also “mask” with an on error resume next and it is worth checking whether checking a box executes any JavaScript on the page.
Only reason I suggested that was because I've done it millions of times myself :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.