VBA getElementsByTagName Issues
Hello,
I am trying to acquaint myself with VBA's getElementsByTagName, but I am struggling to make it work for me. I use Microsoft Office 2013, and have enabled Microsoft Internet Control and Microsoft HTML Object Library. In the following code, I am trying to grab the birthdate and place it in cell A1. However, this code seems to do nothing. It does not seem to even produce an error.
Quote:
Sub Extract_TD_text()
Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long
'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
URL = [Wikipedia page for Rihanna (not allowed to paste HTML here)]
Set IE = New InternetExplorer
With IE
.navigate URL
.Visible = True
'Wait for page to load
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set HTMLdoc = .document
End With
Set TDelements = HTMLdoc.getElementsByTagName("TD")
r = 0
For Each TDelement In TDelements
'Look for required TD elements - this check is specific to VBA Express forum - modify as required
If TDelement.className = "infobox.vcard.plainlist" Then
Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
r = r + 1
End If
Next
IE.Quit
End Sub
Thanks for your time.
1 Attachment(s)
getElementsByTagName fail to catch "td" tag elements
I need help from the smart guys here on the site. The code snippet below don't give any errors and it looks that it works fine, but the TD element collection stays empty and the innerText is empty too. That while the data is showing in a visual browser. GetelementsbyTagName don't catch TDelements, and I can't figure out why ....
Code:
Sub Extract_TD_Text()
Dim URL As String
Dim wMatrix As String
Dim Search As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim myTable As HTMLObjectElement
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim rij As Long
Dim bGetNext As Boolean
URL = "https://www.nasdaq.com/market-activity/stocks/arch"
wMatrix = "sector;industry;1 year target;forward p/e 1 yr.;earnings per share(eps);annualized dividend;ex dividend date;beta"
Set IE = New InternetExplorer
With IE
.Navigate2 URL
.Visible = False 'or True it don't matter
'Wait for page to load
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
rij = 0
bGetNext = False
Set HTMLdoc = .Document
If HTMLdoc.getElementsByTagName("table")(0).getAttribute("class") = "summary-data__table" Then
Set myTable = HTMLdoc.getElementsByTagName("table")(0)
Set TDelements = myTable.getElementsByTagName("td")
For Each TDelement In TDelements
Search = LCase(Trim(TDelement.innerText))
If Search = "" Then
Search = "Nothing Found"
Else
Search = "*" & Search & "*"
End If
If bGetNext = True Then
Debug.Print TDelement.innerText 'Catch the data from the TDelement
bGetNext = False
End If
'Debug.Print TDelement.className '- useful for inspecting the HTML info
If wMatrix Like Search Then
Debug.Print TDelement.innerText 'Show the string where searching for
bGetNext = True 'Trigger to catch the data from the next TDelement
End If
rij = rij + 1
Next
End If
End With
IE.Quit
End Sub
Thanks in advance.