Question

I'm trying to use Excel VBA to perform a very manual process in IE. I've worked with VBA and IE enough to do easy tasks like enter text into fields or interact with buttons, but I'm having a difficult time wrapping my head around this.

I have a list of fields in Excel: First Name, Last Name, Email Address. There are over two thousand rows of information in the spreadsheet. The website I'm working with has a list of over three thousand rows of information (same information with the addition of a checkbox), except it only loads a maximum of 100 rows at a time. Every row of data in Excel exists in the web tool.

I'd like to have my procedure iterate over the names and check the corresponding row on the webpage. The problem is figuring out how to identify the input id for the corresponding checkbox. I'm not very familiar with HTML so my code is very rudimentary... I've spent much of the day writing this:

Sub Test()
Dim str As String
Dim ie As SHDocVw.InternetExplorer
Dim doc As HTMLDocument
Dim tbls As Object
Dim tbl As Object
Dim trow As Object
Dim trs As Object
Dim Cell As Range

Set ie = New SHDocVw.InternetExplorer
ie.navigate "https://test.com"
ie.Visible = True
Set doc = ie.Document
Set tbls = doc.getElementsByTagName("TABLE")
For Each Cell In Range("A2:A2200")
    For Each tbl In tbls
        For Each trow In tbl.Rows
            If InStr(1, trow.Cells(i).innerText, Cell.Offset(0, 1) & ", " & Cell) > 0 Then
                Debug.Print trow.Cells(0).innerText
                Stop
            End If
        Next trow
    Next tbl
Next Cell

End Sub

The above code is able to identify the cell on the page that matches the cell in the workbook, but I can't figure out how to find the id to the associated checkbox. The site is private, so I can't provide a link. See below for what appears to be the relevant piece of source code for the site:

        <div>
        <table cellspacing="0" border="0" id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults" style="border-color:Black;border-width:1px;border-style:Solid;width:100%;border-collapse:collapse;">
            <tr>
                <td colspan="6">
                <table id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_tabPager" border="0" cellpadding="5" width="100%">
                    <tr>
                        <td style="border-style:none;">
                            <a id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_lbPrev" disabled="disabled">Previous</a>
                            &nbsp;&nbsp;
                            <a id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_lbNext" disabled="disabled">Next</a>
                        </td>
                        <td style="border-style:none;" align="right">
                            <b>Showing:</b>
                            <span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow"><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_0" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="10" checked="checked" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_0">10 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_1" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="25" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$1\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_1">25 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_2" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="50" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$2\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_2">50 People</label><input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_3" type="radio" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow" value="100" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$rblShow$3\',\'\')', 0)" /><label for="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_rblShow_3">100 People</label></span>
                        </td>
                    </tr>
                    <tr id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl01_trSelectOptions">
                        <td colspan="2" style="border-style: none;" align="left">

                                <a href="javascript:SetCheckedAll(true);">Select All on Page</a>
                                &nbsp;&nbsp;
                                <a href="javascript:SetCheckedAll(false);">Clear All on Page</a>
                                &nbsp;&nbsp;

                            <a class="link_w_img" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$ctl09&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">
                                    <img src="/images/all.gif" />
                                    <span>Select Entire List</span></a>
                                &nbsp;&nbsp;
                                <a class="link_w_img" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl01$ctl11&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">
                                    <img src="/images/none.gif" />
                                    <span>Clear Entire List</span></a>
                        </td>
                    </tr>
                </table>    
            </td>
            </tr>

            <tr class="gridheader" align="left">
                <th scope="col">Select</th>
                <th scope="col">Last Name</th>
                <th scope="col">First Name</th>
                <th scope="col">Middle Name</th>
                <th scope="col">E-mail Address</th>
                <th scope="col" style="width:0px;">&nbsp;</th>
            </tr>

            <tr class="griditem" align="center">
                <td>
                    <input id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_SelectCheckBox" type="checkbox" name="ctl00$ctl00$all_content$all_content$content$ucUserSearch$gvSearchResults$ctl03$SelectCheckBox" />
                </td>
                <td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Demo1</td>
                <td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Test</td>
                <td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">&nbsp;</td>
                <td align="left" onmouseover="ShowPopup('ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip');" onmouseout="HidePopup();">Demo1@Test.com</td>
                <td style="width:0px;">
                    <div id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_divTip" class="popup">
                        <div class="popupbg">
                            <table>
                                <tr>
                                    <td style="vertical-align:top;">
                                        <span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_lblName" style="font-weight:bold;">Demo1, Test </span>
                                        <br />E-mail:<span id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_lblEmail">Demo1@Test.com</span><br />
                                            <table cellspacing="0" border="0" id="ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_dgDemos" style="border-style:None;border-collapse:collapse;margin-top:-1.25em;">
                                <tr>
                                <td>&nbsp;
                                </td><td>&nbsp;
                                </td>
                        </tr><tr>
                            <td>
                                                            Employee ID: 
                                                        </td><td>
                                                            N/A
                                                        </td>
                        </tr><tr>
                            <td>
                                                            Job Title: 
                                                        </td><td>
                                                            N/A
                                                        </td>
                        </tr><tr>
                            <td>
                                                            Location: 
                                                        </td><td>
                                                            N/A
                                                        </td>
                        </tr>
                    </table>
                                        </td>
                                        <td style="width:50px;vertical-align:top;">
                                            <img src="/images/user_popup.jpg" border="0" width="48" height="48"/>
                                        </td>
                                    </tr>
                                </table>
                            </div>

                </div>
                    </td>

In the above example, the Last Name is Demo1, the First Name is Test, and the Email is Demo1@Test.com, and the input id for the checkbox is ctl00_ctl00_all_content_all_content_content_ucUserSearch_gvSearchResults_ctl03_SelectCheckBox.

My question is, how do I identify the input id after I've identified table row? Or, perhaps that's impossible and I'm going about this the wrong way? Any thoughts will be much appreciated.


Edit

This is what I tried that gives me the Runtime error (Object variable or With block variable not set):

trow.Cells(0).getElementsByTagName("input")(0).Click

I also tried this:

set input = trow.Cells(0).getElementsByTagName("input")(0)
input.Click

And finally, both these produce the same error:

Debug.Print trow.Cells(0).getElementsByTagName("input")(0).innerText
Debug.Print trow.Cells(0).getElementsByTagName("input")(0)

This is the output of the Debug.Print in my original code:

Demo1, Test
E-mail: Demo1@Test.com


Employee ID: N/A
Job Title: N/A
Location: N/A

The only pieces I care about (in the output) are the first two lines, the email and name of the individual.

Was it helpful?

Solution

This test worked for me. Looks like the rows you want all have class="griditem", so you can check all table rows for that class name. When you find a match on that and on your cell values, you should be able to find the checkbox as shown.

Sub Tester()

Dim d As New HTMLDocument, trs, rw

    d.body.innerHTML = _
       "<section><table><tr class='griditem'><td>" & _
       "<input type='checkbox' id='id_1'></td>" & _
       "<td>Foo</td></tr><tr class='griditem'><td>" & _
       "<input type='checkbox' id='id_2'></td>" & _
       "<td>Bar</td></tr></table></section>"

    Set trs = d.getElementsByTagName("tr")
    For Each rw In trs
        If rw.className = "griditem" Then
            'check cell values for match....
            Debug.Print rw.Cells(0).getElementsByTagName("input")(0).ID
        End If
    Next rw

End Sub

OTHER TIPS

Give a try to this.

Set ElementCol = ie.Document.getElementsByTagName("input")

For Each btnInput In ElementCol

'your code here 


Next btnInput
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top