如何将一张桌子直接一个接一张地放置 [英] How to place one table directly after another

查看:46
本文介绍了如何将一张桌子直接一个接一张地放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 VBA 构建一个 Word 文档.我逐行添加一个表格;完成后,我想插入一个空行/段落,然后开始一个新表格.但是当我在表格之后添加段落时,插入点出现在段落标记之前,所以下一个表格被添加到那里,并成为第一个表格的一部分.

I'm building a Word document in VBA. I add a table row by row; once it's complete, I want to insert a blank line/paragraph and then start a new table. But when I add the paragraph after the table, the insertion point appears before the paragraph marker, so the next table is added there, and becomes part of the first table.

Set HeaderTableId = WordDoc.Tables.Add(Range:=wrdSel.Range, numcolumns:=3, numrows:=1, AutoFitBehavior:=wdWord9TableBehavior)

Set RowId = HeaderTableId.Rows(1)
RowId.Cells(1) = LeftHeader
RowId.Cells(2).Range.Font.Bold = True
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
RowId.Cells(2) = CentreHeader
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
RowId.Cells(3) = RightHeader
' (this table only has one row)
With HeaderTableId.Range
    .Collapse (WdCollapseDirection.wdCollapseEnd)
    .Move Unit:=wdCharacter, Count:=3
    .Select
    .InsertParagraph
End With

最后的 .InsertParagraph 在表格之后正确插入了一个空白段落,但插入点在段落标记之前.我也试过插入分页符,但它有同样的问题.我不知道如何将插入点移到最后.

The final .InsertParagraph correctly inserts a blank paragraph after the table, but the insertion point is then before the paragraph marker. I've also tried inserting a page break, but it has the same problem. I can't work out how to move the insertion point to the end.

推荐答案

为了测试,我不得不充实"你的代码 - 我已经在下面粘贴了整个测试代码.

I had to "flesh out" your code in order to test - I've pasted the entire test code below.

在第一个表格之后插入第二个表格的关键,用段落标记分隔以确保两个表格不会合并:

The key to inserting a second table following the first, separated by a paragraph mark to ensure the two tables are not merged:

有必要折叠表格Range 两次:插入新段落之前和之后一次.

It's necessary to collapse the table Range twice: once before and once after inserting the new paragraph.

问题中的代码使用了.Move,不清楚Range是如何变化的.如果我要使用移动",我会使用 .MoveStart,这将保持折叠范围折叠,但对于这个问题,我更喜欢 Collapse.(还有 MoveEnd,它将扩展折叠的 Range 以包含内容.)

The code in the question uses .Move, which is unclear as to how the Range is changed. If I were to use a "move" I'd go with .MoveStart which will keep a collapsed range collapsed, but for this problem I prefer Collapse. (There's also MoveEnd, which will extend a collapsed Range to include content.)

我的版本还有什么不同:

What's also different in my version:

  • 它使用独立于任何表格范围的工作范围" - 这是个人偏好
  • 它使用 InsertAfter vbCr 插入新段落 - 再次说明个人偏好:我总是知道插入的内容是 Range 对象的一部分.有时,使用 Insert 方法,新内容可能不是 Range 的一部分,但我知道它与 InsertAfterInsertBefore
  • it uses a "working Range" that's independent of any table range - this is personal preference
  • it uses InsertAfter vbCr for inserting the new paragraph - again, personal preference: I always know that what's inserted is part of the Range object. Sometimes, with Insert methods the new content may not be part of the Range, but I know it is with InsertAfter and InsertBefore

代码:

Sub InsertSuccessiveTables()
    Dim HeaderTableId As word.Table, nextTable As word.Table
    Dim RowId As word.Row
    Dim workRange As word.Range
    Dim WordDoc As word.Document

    Set WordDoc = ActiveDocument
    Set workRange = Selection.Range
    Set HeaderTableId = WordDoc.Tables.Add(Range:=workRange, numcolumns:=3, numrows:=1, AutoFitBehavior:=wdWord9TableBehavior)

    Set RowId = HeaderTableId.Rows(1)
    RowId.Cells(1).Range.text = "Left"
    RowId.Cells(2).Range.Font.Bold = True
    RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    RowId.Cells(2).Range.text = "Center"
    RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    RowId.Cells(3).Range.text = "Right"
    ' (this table only has one row)
    Set workRange = HeaderTableId.Range
    With workRange
        .Collapse WdCollapseDirection.wdCollapseEnd
        .InsertAfter vbCr 'ANSI 13
        .Collapse WdCollapseDirection.wdCollapseEnd
    End With
    Set nextTable = workRange.Tables.Add(workRange, 1, 4, AutoFitBehavior:=wdWord9TableBehavior)
End Sub

这篇关于如何将一张桌子直接一个接一张地放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆