getElementsByTagname

  • Thread starter Thread starter senderj
  • Start date Start date
S

senderj

I am writing a marco to put a html table into cells. I am able to
Set doc_tables = ie.Document.getElementsByTagname("table")
Set tab_rows = doc_tables(7).getElementsByTagname("tr")
But I hit an error when trying to
Set tab_cells = tab_rows.getElementsByTagname("td")

Where can I read more about this kind of coding? Is this called
ActiveX object DOM?
 
Have you tried

Set doc_tables = ie.Document.getElementsByTagname("table")
Set tab_cells = doc_tables(7).getElementsByTagname("td")


Your line:
Set tab_cells = tab_rows.getElementsByTagname("td")

does not work because tab_rows is a collection of elements, not a single
element.

Set tab_cells = tab_rows(1).getElementsByTagname("td")

(eg) should work.

Tim
 
I am writing a marco to put a html table into cells. I am able to
    Set doc_tables = ie.Document.getElementsByTagname("table")
    Set tab_rows = doc_tables(7).getElementsByTagname("tr")
But I hit an error when trying to
    Set tab_cells = tab_rows.getElementsByTagname("td")

Where can I read more about this kind of coding? Is this called
ActiveX object DOM?

You might also look at constructions such as:

Set doc_tables = ie.document.getElementsByTagname("table")
ActiveCell = doc_tables(7).innertext
Activecell.Offset(1,0).Select
ActiveCell = doc_tables(7).Rows(0).innertext
Activecell.Offset(1,0).Select
ActiveCell = doc_tables(7).Rows(0).Cells(2).innertext

and see what they may have to offer...Ron
 
Thank you for all the replies. My final coding looks like this, but
with occasional problem:

Sub Get_page3(conn)
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.Navigate conn
Do Until Not .Busy
DoEvents
Loop
End With
Set doc_tables = ie.document.getElementsByTagname("table")
Set tab_rows = doc_tables(7).Rows
nRow = 0
For Each rr In tab_rows
If nRow = 0 Then 'skip header row
Else
cc = 0
For Each cel In rr.Cells
Selection.Offset(nRow - 1, cc).NumberFormat =
"General"
Selection.Offset(nRow - 1, cc).Value = cel.innertext
cc = cc + 1
Next
End If
nRow = nRow + 1
Next
Selection.Offset(tab_rows.Length - 1, 0).Select
End Sub

It is called by another macro 4 times, 1 for each page I want.
Sometimes it runs alright from first to last without problem.
Sometimes it stops in one of the page with "object variable or with
block variable not set" at the "Set tab_rows = doc_tables(7).Rows"
statement. But after entering debug mode, if I click Run, it continues
to run without the problem. Any idea?
 
Sounds like you have a timing problem - the table you need hasn't yet been
rendered when you try to get a reference to it.

Try replacing your "wait loop" with this:

Do While ie.document.readyState <> "complete"
DoEvents
Loop

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top