-
Excel VBA + IE
Hello,
I have this code that works:
Code:
Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
But program crashes when it is not present so tried doing it this way and it doesn't work:
HTML also inside
Code:
'<div id="report_165215891151811269_catch"><span class="nodatafound">No Non-Res customer found by the search criteria.</span>
'</div>
Dim nonresidential as Object
Dim rowj as integer ' this is in a for loop to move through rows
nonresidential = objIE.document.getElementById("report_165215891151811269_catch")
If nonresidential Is Nothing Then
Else
Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
End If
Also why is it that html code would produce an odd name like this: report_165215891151811269_catch for every non residential address- just curious?
Thank you!
-
Re: Excel VBA + IE
for vba, probably should be
Code:
set nonresidential = objIE.document.getElementById("report_165215891151811269_catch")
used to get the job done, but in later versions of ie this no longer works, you may need to loop through a subset of the elements, till you find a match or not, something like
Code:
for each ele in objIE.document.All
if ele.id = "report_165215891151811269_catch" then nonresidential = true: Exit For
next
if nonresidential then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
you maybe able to use some tagname to reduce the number of elements to iterate through, depends if the length (count) of the elements in the document as to whether it is relevant
-
Re: Excel VBA + IE
Thanks for the answer. It does work but the program gives a runtime error 424 object required when it encounters an address that is not found in the system with this highlighted in yellow:
Code:
Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
Code:
When I use On Error Resume Next the program works fine but would like to avoid any potential errors. Any ideas? Thank you again
Set nonresidential_elements = objIE.document.getElementsByTagName("report_165215891151811269_catch")
''''''''''''''''' this is what I tried below- doesn't work
'For Each nonresidential In nonresidential_elements
' If nonresidential.getAttribute("span") = "nodatafound" Then
' Cells(rowj, 4) = nonresidential.innerText
' End If
'Next nonresidential
'''''''''''''''your code works below but gives runtime error
For Each ele In objIE.document.all
If ele.ID = "report_165215891151811269_catch" Then nonresidential = True: Exit For
Next
If nonresidential Then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
-
Re: Excel VBA + IE
from your code, i am not sure why you should get an error, but if you are running within a loop you would need to set the variable to false before each, if this is not the case, more information is required
Code:
nonresidential = False
For Each ele In objIE.document.all
If ele.ID = "report_165215891151811269_catch" Then nonresidential = True: Exit For
Next
If nonresidential Then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText