How to? referencing table with differing number of cells per row


K

ker_01

I have a table that has 17 /or/ 19 rows, and some rows have 2 cells, while
others have 3.

I think that the best way to cycle through the table to pull every value is
something like:

For EachColumn = 1 to 19
For EachRow = 1 to 3
'do stuff
Next EachRow
Next EachColumn

but I need to only "do stuff" if that cell actually exists; right now it
hits the first row where there are 2 cells instead of three, and throws a
runtime error 5941 "the requested member of the collection does not exist".

What is the appropriate syntax to determine if the table cell exists before
I try to process anything related to that cell? Something like:

If MyWordDocument.Tables(1).Cell(EachRow, EachColumn).Exists then...

Many thanks!
Keith
 
Ad

Advertisements

M

macropod

Hi Keith,

try:
Sub Test()
Dim oTbl As Table, i As Integer, j As Integer
For Each oTbl In ActiveDocument.Tables
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
' Do stuff
Next j
Next i
Next oTbl
End Sub
 
K

ker_01

I like the approach- but I need a little help with the syntax (I'm an Excel
VBA'er, and don't know the Word object model).

For j = 1 To oTbl.Columns.Count appears to give the column count for
the entire table, rather than just for that current row. As a result, it
still errors when it hits a row that only contains two cells.

For testing, I tried variations of For j = 1 To
oTbl.Row(1).Columns.Count but so far have been unsuccessful :(

Many thanks for your continued help,
Keith

macropod said:
Hi Keith,

try:
Sub Test()
Dim oTbl As Table, i As Integer, j As Integer
For Each oTbl In ActiveDocument.Tables
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
' Do stuff
Next j
Next i
Next oTbl
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


ker_01 said:
I have a table that has 17 /or/ 19 rows, and some rows have 2 cells, while
others have 3.

I think that the best way to cycle through the table to pull every value is
something like:

For EachColumn = 1 to 19
For EachRow = 1 to 3
'do stuff
Next EachRow
Next EachColumn

but I need to only "do stuff" if that cell actually exists; right now it
hits the first row where there are 2 cells instead of three, and throws a
runtime error 5941 "the requested member of the collection does not exist".

What is the appropriate syntax to determine if the table cell exists before
I try to process anything related to that cell? Something like:

If MyWordDocument.Tables(1).Cell(EachRow, EachColumn).Exists then...

Many thanks!
Keith
 
Ad

Advertisements

K

ker_01

I think I've got it: For Each Cell In .Tables(1).Rows(tRow).Cells

The rest of my code is returning gobblygook, but at least it isn't crashing
on the table. If I can't figure out why it is returning gobblygook I'll post
a new thread with more details.

Thanks!
Keith

ker_01 said:
I like the approach- but I need a little help with the syntax (I'm an Excel
VBA'er, and don't know the Word object model).

For j = 1 To oTbl.Columns.Count appears to give the column count for
the entire table, rather than just for that current row. As a result, it
still errors when it hits a row that only contains two cells.

For testing, I tried variations of For j = 1 To
oTbl.Row(1).Columns.Count but so far have been unsuccessful :(

Many thanks for your continued help,
Keith

macropod said:
Hi Keith,

try:
Sub Test()
Dim oTbl As Table, i As Integer, j As Integer
For Each oTbl In ActiveDocument.Tables
For i = 1 To oTbl.Rows.Count
For j = 1 To oTbl.Columns.Count
' Do stuff
Next j
Next i
Next oTbl
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


ker_01 said:
I have a table that has 17 /or/ 19 rows, and some rows have 2 cells, while
others have 3.

I think that the best way to cycle through the table to pull every value is
something like:

For EachColumn = 1 to 19
For EachRow = 1 to 3
'do stuff
Next EachRow
Next EachColumn

but I need to only "do stuff" if that cell actually exists; right now it
hits the first row where there are 2 cells instead of three, and throws a
runtime error 5941 "the requested member of the collection does not exist".

What is the appropriate syntax to determine if the table cell exists before
I try to process anything related to that cell? Something like:

If MyWordDocument.Tables(1).Cell(EachRow, EachColumn).Exists then...

Many thanks!
Keith
 

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

Top