Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA get parent node of element

Tags:

html

ms-word

vba

I have a script that parses an HTML page to find an element with a specified ID. What I need is to get the parent node (actually, I need to go 2 parent nodes up) so that I can either cycle through all the links in the table to find the link I want OR so I can parse the HTML of the entire table to find the link I want.

Sub testmacro()

'Constants
Const pdlID = "Id of the element"
Const urlbase = "what ever url"

'containers
Dim web As Object
Dim prompt As String
Dim pdlTbl As Object
Dim pdllink As Object


'Get URL
prompt = InputBox("Paste the URL here: ", "URL")

'create IE instance
Set web = CreateObject("InternetExplorer.Application")
web.Visible = True
web.Navigate prompt

Do While web.Busy Or web.ReadyState <> 4
    DoEvents
Loop

'get IE document
Set pdlTbl = web.Document
Set pdlTbl = pdlTbl.getelementbyid(pdlID)

'Get parent node of element

web.Quit
Set web = Nothing

End Sub

Ive tried to find information on getting parent nodes but I couldn't so any help on locating the parent nodes would be great.

Additionally, I was having major issues trying to cycle through elements in the document. I grabbed many many code samples from this site and they were all returning the same errors "Object does not support this method" or something along the lines. It was highlighing the first line in loops that looked like this.

for each a in pdltbl

next a

This error came out no matter which tag I tried to use or the type of element I looked through (pdlID refers to a TD element for reference)

Basically, I just want to be able to search through the table to find a link with a specific URL (based off the url in prompt)

like image 270
gNerb Avatar asked Sep 15 '25 07:09

gNerb


1 Answers

Set pdlTbl = pdlTbl.getelementbyid(pdlID)
Set parentParent = pdlTbl.parentElement.parentElement

to get all the links in a table:

Set allLinks = tbl.getElementsByTagName("a")
For Each a in allLinks
    '...
Next a
like image 199
Tim Williams Avatar answered Sep 17 '25 01:09

Tim Williams