ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   getElementsByTagname (https://www.excelbanter.com/excel-programming/424282-getelementsbytagname.html)

senderj

getElementsByTagname
 
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?

Tim Williams[_2_]

getElementsByTagname
 
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


"senderj" wrote in message
...
I am writing a marco to put a html table into cells. I am able to

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




ron

getElementsByTagname
 
On Feb 17, 6:24*pm, senderj wrote:
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

senderj

getElementsByTagname
 
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?

Tim Williams[_2_]

getElementsByTagname
 
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

"senderj" wrote in message
...
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?





All times are GMT +1. The time now is 02:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com