问题
How can I reliably move down the rows in a Word table?
Here's the structure of the table. Note that the first and second columns can both have multiple lines and paragraphs.
Rule ID 1
Logic Date must be equal to or greater than 01-Jan-2012
Discrepancy Date is before 01-Jan-2012
message
Test case 1.1 Create form where Date is before 01-Jan-2012
Verify discrepancy message appears.
Print form.
Test case 1.2 Create form where Date is equal to 01-Jan-2012.
Verify no discrepancy message appears.
Print form.
Test case 1.3 Create form where Date is after 01-Jan-2012.
Verify no discrepancy message appears.
Print form.
I have tried a number of methods to move down the table.
When I tried Selection.MoveDown
with unit:=wdLine
(below) I ran into problems when column 1 contained a word wrap.
Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdMove
When I tried Selection.MoveDown
with unit:=wdParagraph
(below), I ran into problems when column 2 contained multiple paragraphs.
Selection.MoveDown unit:=wdParagraph, Count:=3
unit:=wdRow
does not appear to be a valid parameter for Selection.MoveDown
.Selection.Cells(1).RowIndex
is a read-only parameter
Does anyone know of a simple method to move down the table one row at a time that would handle both the word wraps in column 1 and the multiple paragraphs in column 2?
回答1:
Try something like this. It's a generalized algorithm for looping through each row and column of all the tables in a Word document. Modify as needed (untested code):
Sub ModifyAllTables()
Dim oTbl As Table
Dim oRow As Row
Dim oRng As Range
For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Rows
' Select the cell in column 2.
Set oRng = oRow.Cells(2).Range
' and do something with it...
'e.g. oRng.TypeText "modified"
Next
Next
End Sub
回答2:
Sub NextRow()
Dim c As Long, r As Long
With Selection
'ignore if not in table
If .Information(wdWithInTable) Then
c = .Columns(1).Index
r = .Rows(1).Index
If .Rows(1).Parent.Rows.Count >= r + 1 Then
.Rows(1).Parent.Rows(r + 1).Cells(c).Range.Select
End If
End If
End With
End Sub
来源:https://stackoverflow.com/questions/14327072/moving-down-a-row-in-a-word-table-containing-multi-paragraph-cells