Sai
Sai

Reputation: 221

VBA Internet Explorer clicking on Text Error

I am new to VBA and it would be great if you can help me on resolving this issue. I am trying to click on a Text on an IE SharePoint webpage. I am am able to navigate to IE browser, but I am getting a VBA error for clicking the text "Americas" highlighted in Yellow in attached Screenshot. I need help with the IE.Document part of the code at the end of VBA code below. I assume GetElementbyID and GetElementByTagName are correct from HTML code below. Error - Method Document of Object "IEwebBrowser"Failed

VBA Code:

 Private Sub UploadFile()

    Dim i As Long
    Dim IE As Object
    Dim Doc As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim buttonCollection As Object

    Dim AllSpanElements
    Dim Span

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

    ' Send the form data To URL As POST binary request
    IE.navigate "URL"

    ' Wait while IE loading...
    While IE.Busy
            DoEvents
    Wend

    ' I AM GETTING ERROR HERE
        Set AllSpanElements = IE.Document.getElementById("ext-gen1271").getElementsByTagName("div")
        AllSpanElements.Click

        Set IE = Nothing
        Set objElement = Nothing
 End Sub

HTML CODE

<table class="x-grid-table x-grid-table-resizer" border="0" cellspacing="0" cellpadding="0" style="width:10000px;"><tbody>
<tr class="x-grid-row x-grid-row-selected x-grid-row-focused" id="ext-gen1271">
<td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1030   x-grid-cell-first">
<div class="x-grid-cell-inner " style="text-align: left; ;"><img src="data:image/gif;base64,R0lGODlhAQABAID/AMDA" class="x-tree-elbow-plus x-tree-expander">
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAA" class="x-tree-icon x-tree-icon-parent ">
Americas</div>
</td>
</tr>
</tbody>
</table>

Upvotes: 0

Views: 1116

Answers (1)

Ryan Wildry
Ryan Wildry

Reputation: 5677

Give this a shot, I cleaned up the code slightly and I'm trying a slightly different approach. Basically I'm iterating over each element on the page, then clicking it when the InnerText has "Americas" contained with in it.

It may not be the InnerText Property you want to check, it might be the Value or Title so you will need to check that.

Here's the code:

Private Sub UploadFile()
   Dim IE                  As Object: Set IE = CreateObject("InternetExplorer.Application")
   Dim Elements            As Object
   Dim Element             As Object

   With IE
       .Visible = True
       ' Send the form data To URL As POST binary request
       .navigate "URL"

       ' Wait while IE loading...
       While .Busy Or .readystate <> 4
           Application.Wait (Now() + TimeValue("00:00:01"))
           DoEvents
       Wend
   End With

   ' I AM GETTING ERROR HERE
   Set Elements = IE.Document.getElementsByTagName("*") ' * is all elements
   For Each Element In Elements
       If InStr(1, Element.innerText, "Americas") > 0 Then ' If the element has the word Americas...click it
           Element.Focus
           Element.Click
           Element.FireEvent ("OnClick")
       End If
   Next

   'Clean up
   Set IE = Nothing
End Sub

Upvotes: 0

Related Questions