【问题标题】:Copying table rows with merged cells复制包含合并单元格的表格行
【发布时间】:2013-01-30 21:21:53
【问题描述】:

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

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

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

但是,可以从用户界面执行此操作,所以它一定是可能的!

【问题讨论】:

  • 请在下方添加您的答案。

标签: vba ms-word runtime-error


【解决方案1】:

这是我克服问题的方法(在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2021-01-09
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多