How to copy a word table multiple times to a specific place using VB.Net 2010?

I have viewed the other responses and these seem just not to work.

I am working on a VB.Net 2010 program using the Interop class.

We are having problems trying to duplicate a table more than 1 time to be put in the place we want.

It is either merging it with the original table in the Word Doc or putting it into the next table.

Is there a clear explanation on how a table can be duplicated 2-3 times?

It copies the first table fine but the 2nd is inserted into the table following the original table.

Imports Word = Microsoft.Office.Interop.Word
...
We are currently using functions as:
MyCopyFromRange.Select()
MyCopyFromRange.Copy()

Rng = MyCopyFromRange
Rng.SetRange(MyCopyFromRange.End, MyCopyFromRange.End)

For MyCnt = 1 To pNoOf
WordApp.Selection.InsertAfter(MySepStr) ' not then it appends to the DevTable

WordApp.Selection.MoveDown() ' Move down needed if not appends to origina table

WordApp.Selection.Paste() ' Manual paste works without the move down this does not. It puts it in the next table
..
Next

Dani AI

Generated

Problem described by is common: programmatic paste often ends up inside the source/next table because the insertion point is still "in a table" or Selection/clipboard operations are used. 's idea of adding a blank paragraph is on the right track, and 's array idea can help for data handling, but a more reliable Interop approach is to avoid Selection/clipboard entirely and insert copies via Range.FormattedText (this copies the whole table including formatting without using the clipboard).

Key points:

  • Use a bookmark or an explicit Range for the exact insertion location.
  • Collapse that Range to its end, ensure it is not inside a table (if it is, insert a paragraph first), then assign the source table's FormattedText into that destination Range.
  • After each insert, move/update the bookmark so the next copy is placed after the newly inserted table.

Example (VB.NET + Interop):

' Assumes "doc" is a Microsoft.Office.Interop.Word.Document and a bookmark "InsertHere" exists
Dim srcTbl As Word.Table = doc.Tables(1)
Dim copies As Integer = 3
Dim bm As String = "InsertHere"

For i As Integer = 1 To copies
    Dim dest As Word.Range = doc.Bookmarks(bm).Range
    dest.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

    If dest.Information(Word.WdInformation.wdWithInTable) Then
        dest.InsertParagraphAfter()
        dest.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
    End If

    ' Copy table without clipboard
    dest.FormattedText = srcTbl.Range.FormattedText

    ' Update bookmark to the end of the inserted content
    If doc.Bookmarks.Exists(bm) Then doc.Bookmarks.Item(bm).Delete()
    doc.Bookmarks.Add(bm, dest)
Next

Troubleshooting notes:

  • If tables still merge, confirm the destination Range reports False for wdWithInTable before insertion.
  • For server-side or headless scenarios, avoid Interop (use Open XML SDK) since Interop requires Word installed and a desktop session.
  • If the source table contains fields/cross-references, update fields after insertion (Document.Fields.Update()).

This method is deterministic, avoids clipboard quirks that cause tables to merge, and lets a fixed bookmark or placeholder control exact placement.

Recommended Answers

All 2 Replies

HI,
With Visual Basic I have had the same problem. May I suggest and I am not sure it will work but creating an array to store the data may solve your situation. It worked for me in a simular situation :-).

Have you thought of entering a blank line after you copy so that the next table would be in a separate paragraph?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.