Navigating logical table rows

W

Walter Briscoe

I use Word 2003 SP1 on Windows XP Professional SP2.
I am very new to VBA programming and usually produce code by recording a
macro and seeing what is done.

I have a document which consists of one table.
The table consists of "logical" rows.

A "logical" row is either a single physical row as in:
R1C1 R1C2 R1C3 R1C4 ...
or a multiple physical row
R1C1 R1C2 R1C3 R1C4 ...
R2C3 R2C4 ...
R3C3 R3C4 ...

I want to pad some logical rows identified by find.execute as in
R1C1 R1C2 find R1C4 ...
or
R1C1 R1C2 R1C3 R1C4 ...
R2C3 find ...
R3C3 R3C4 ...

with bottom padding as in:
R1C1 R1C2 find R1C4 ...
botm botm botm botm ...
or

R1C1 R1C2 R1C3 R1C4 ...
R2C3 find ...
R3C3 R3C4 ...
botm botm ...

The last row happens to be of the form
R1C1 R1C2 find R1C4 ...
and a found cell can be anywhere in a logical row.

I have struggled without success to identify the relevant physical rows.

Incidentally, I was surprised to find that, at the last cell on the last
row, Selection.MoveRight Unit:=wdCell (which the Tab key maps to) causes
a new empty row to be appended.) I do not find this described in the
help for MoveRight.

I also have not found a key or a function which causes a Selection to be
closed as a mouse click outside the selection would do.

If this is not an appropriate group, I apologise for not kibitzing for
long enough and would value suggestions of a better one.

I would also value pointers to any good tutorials on table navigation.
 
S

StevenM

To: Walter Briscoe,

Sub NavigatingRowsAndColumns()
Dim oTable As Table
Dim row As Long
Dim col As Long
Dim s As String

Set oTable = ActiveDocument.Tables(1)
For row = 1 To oTable.rows.count
For col = 1 To oTable.rows(row).Cells.count
s = oTable.rows(row).Cells(col).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next col
Next row
End Sub

The above will not work if cells in the table have been split or merged.
Also, there are other ways to do this, but the above macro illustrates a
simple method.

As for the rest of your message, I couldn't quite figure out what you
wanted. Perhaps you could try again to state what you're wanting. (Also, you
realize that you are in a Word newgroup and not an Excel newsgroup, yes?)

Steven Craig Miller
 
W

Walter Briscoe

In message <[email protected]> of Thu,
21 Aug 2008 06:34:00 in microsoft.public.word.tables, StevenM

Thanks, Steven for a really prompt reply.
To: Walter Briscoe,

Sub NavigatingRowsAndColumns()
Dim oTable As Table
Dim row As Long
Dim col As Long
Dim s As String

Set oTable = ActiveDocument.Tables(1)
For row = 1 To oTable.rows.count
For col = 1 To oTable.rows(row).Cells.count

That line generates "Cannot access individual rows in this collection
because the table has vertically merged cells".
I got a similar failure when I nested for each oTable, for each oRow,
and for each oCol
s = oTable.rows(row).Cells(col).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next col
Next row
End Sub

The above will not work if cells in the table have been split or merged.
Also, there are other ways to do this, but the above macro illustrates a
simple method.

I guess I need a less simple method.
As for the rest of your message, I couldn't quite figure out what you
wanted. Perhaps you could try again to state what you're wanting. (Also, you
realize that you are in a Word newgroup and not an Excel newsgroup, yes?)

I view this as 2 rows:
R1aC1 R1aC2 R1aC3 R1aC4
R1bC3
R2C1 R2C2 R2C3 R2C4

Word views it as
R1C1 R1C2 R1C3 R1C4
R2C1
R3C1 R3C2 R3C3 R3C4

I do understand I am dealing with Word tables. I reckon I have
vertically split cells which Word views as vertically merged cells. The
difference is on how you get there. ;)
Steven Craig Miller

[snip]
 
S

StevenM

To: Walter Brisoe,

As I understand it, one has only three possibilities.

The first example merely repeats what I sent before.

Sub NavigatingTableViaRows()
Dim oTable As Table
Dim row As Long
Dim col As Long
Dim s As String

Set oTable = ActiveDocument.Tables(1)
For row = 1 To oTable.rows.count
For col = 1 To oTable.rows(row).Cells.count
s = oTable.rows(row).Cells(col).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next col
Next row
End Sub

Sub NavigatingTableViaColumns()
Dim oTable As Table
Dim row As Long
Dim col As Long
Dim s As String
Set oTable = ActiveDocument.Tables(1)
For col = 1 To oTable.Columns.count
For row = 1 To oTable.Columns(col).Cells.count
s = oTable.Columns(col).Cells(row).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next row
Next col
End Sub

Sub NavigatingTableViaCells()
Dim oTable As Table
Dim cell As Long
Dim s As String
Set oTable = ActiveDocument.Tables(1)
For cell = 1 To oTable.Range.Cells.count
s = oTable.Range.Cells(cell).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next cell
End Sub

Steven Craig Miller

Walter Briscoe said:
In message <[email protected]> of Thu,
21 Aug 2008 06:34:00 in microsoft.public.word.tables, StevenM

Thanks, Steven for a really prompt reply.
To: Walter Briscoe,

Sub NavigatingRowsAndColumns()
Dim oTable As Table
Dim row As Long
Dim col As Long
Dim s As String

Set oTable = ActiveDocument.Tables(1)
For row = 1 To oTable.rows.count
For col = 1 To oTable.rows(row).Cells.count

That line generates "Cannot access individual rows in this collection
because the table has vertically merged cells".
I got a similar failure when I nested for each oTable, for each oRow,
and for each oCol
s = oTable.rows(row).Cells(col).Range.Text
s = Left(s, Len(s) - 2)
MsgBox s
Next col
Next row
End Sub

The above will not work if cells in the table have been split or merged.
Also, there are other ways to do this, but the above macro illustrates a
simple method.

I guess I need a less simple method.
As for the rest of your message, I couldn't quite figure out what you
wanted. Perhaps you could try again to state what you're wanting. (Also, you
realize that you are in a Word newgroup and not an Excel newsgroup, yes?)

I view this as 2 rows:
R1aC1 R1aC2 R1aC3 R1aC4
R1bC3
R2C1 R2C2 R2C3 R2C4

Word views it as
R1C1 R1C2 R1C3 R1C4
R2C1
R3C1 R3C2 R3C3 R3C4

I do understand I am dealing with Word tables. I reckon I have
vertically split cells which Word views as vertically merged cells. The
difference is on how you get there. ;)
Steven Craig Miller

[snip]
 

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