4

我需要插入一组行的多个副本,其格式如下所示。

+-------------------------------------------------- +
| 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 | |
+-------------------------------------------------- +

在案例 A 中,我需要在第 4 行之前插入第 1-3 行的多个副本。

在案例 B 中,我需要在表格末尾插入第 4-6 行的多个副本。

table.rows(n) 方法不起作用并给出以下错误:

运行时错误“5991”:

无法访问此集合中的各个行,因为该表具有垂直合并的单元格

但是,可以从用户界面做到这一点,所以它一定是可能的!

4

1 回答 1

5

这是我克服问题的方法(在 Word 2007 和 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
于 2013-02-01T11:43:09.030 回答