Reputation: 2762
I needed to insert multiple copies of a group of rows formatted as shown below.
+-------------------------------------------------+ | 1,1 | +-------------------------------------------------+ | | 2,2 | | 2,4 | | | 2,1 +---------+ 2,3 +---------+ 2,5 | | | 3,2 | | 3,4 | | +-------------------------------------------------+ | 4,1 | +-------------------------------------------------+ | | 5,2 | | 5,4 | | | 5,1 +---------+ 5,3 +---------+ 5,5 | | | 6,2 | | 6,4 | | +-------------------------------------------------+
In case A, I needed multiple copies of rows 1-3 inserted before row 4.
In case B, I needed multiple copies of rows 4-6 inserted at the end of the table.
The table.rows(n) method doesn't work and gives the following error :
Run-time error '5991':
Cannot access individual rowsin this collection because the table has vertically merged cells
However, one can do this from the user interface, so it must be possible!
Upvotes: 4
Views: 3869
Reputation: 2762
Here is how I overcame the problem (tested in Word 2007 and Word 2003)
Sub CaseA()
Dim D As Document, T As Table
Set D = ActiveDocument
Set T = D.Tables(1) ' select the first table in the document
' select from the start of row 1 to the start of row 4
D.Range(T.Cell(2, 1).Range.Start, T.Cell(4, 1).Range.Start).Select
Selection.Copy ' copy the rows
' move the insertion point to the start of row 4
Selection.Collapse wdCollapseEnd
Selection.Paste ' insert a copy
' (can do this as many times as you want)
End Sub
Sub CaseB()
Dim D As Document, T As Table
Set D = ActiveDocument
Set T = D.Tables(1)
' select from the start of row 5 to the end of the last cell present in row 6
' plus 1 character for column 5 plus 1 character to move outside the table
D.Range(T.Cell(5, 1).Range.Start, T.Cell(6, 4).Range.End + 2).Select
Selection.Copy ' copy the rows
' move the insertion point just outside the table
Selection.Collapse wdCollapseEnd
Selection.Paste ' insert a copy
End Sub
Upvotes: 5