1
votes

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

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.

1

1 Answers

0
votes

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:

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

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:

  • 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

The code:

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