如何在另一个 table 和 Vba 之后添加一个新词 table 而不会嵌套或链接它们

How to add a new word table after another table with Vba without them getting nested or linked

我正在尝试使用 vba 添加几个 table 到 word 文档。到目前为止,我的代码可以添加一个 table。但是当我添加第二个时,它不会添加 table 而是将单元格添加到之前的 table.

但我需要在每个页面上单独 tables ...

我目前拥有的:

Sub addIntroTable(wdDoc As Document)
    Dim myRange As Object
    Set myRange = wdDoc.Content
    myRange.Collapse Direction:=wdCollapseEnd
    With wdDoc
        .Tables.Add Range:=myRange, NumRows:=3, NumColumns:=1
        With .Tables(.Tables.Count)
            .Cell(1, 1).SetHeight RowHeight:=100, _
                                  HeightRule:=wdRowHeightExactly
            ... more table formatting (shortened for reading)..
            .Cell(1, 1).Range.Text = "some text"
        End With
    End With
End Sub

我还有一些针对不同 table 风格的潜艇,但它们都做同样的事情。当调用单个 sub 时,它会将 table 添加到文档的末尾,并且它后面仍然有一个段落分隔符。

当我在它之后立即调用这个或其他子项之一时,这些行只是添加到前一个 table。然后将格式应用于整个 table.

我必须更改插入点吗?还是我漏掉了什么?

是的,您需要移动插入点。

每个 table 之间至少需要有一个段落。您问题中的代码在文档末尾添加了一个 table 。这将在 table 之后留下一个段落。如果您在该段的开头插入另一个 table,Word 将加入两个 table。

所以您需要做的是添加另一个段落,然后将您的 table 添加到新段落中。

  dim MyRange as Range  
    MyRange = oWordTable.Range
    MyRange.Collapse(WdCollapseDirection.wdCollapseEnd)
    MyRange.Move(WdUnits.wdCharacter, 1)
    MyRange.Select()
    MyRange.InsertParagraph()
    MyRange.InsertBreak(WdBreakType.wdPageBreak)