Copy table rows with merged cells

I needed to insert multiple copies of a group of strings 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 a few copies of lines 1-3 inserted before line 4.

In case B, I needed several copies of rows 4-6 inserted at the end of the table.

The table.rows (n) method does not work and gives the following error:

Runtime Error '5991':

It is not possible to access individual rows in this collection because the table has vertically merged cells.

However, this can be done from the user interface, so it should be possible!

+5
source share
1 answer

This 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
+5
source

All Articles