How to copy content of bookmark several times in same document

Hi there,

I have a Word document that contains some static text and a table with bookmarks. The table is the content of a bookmark itself. My task is to write a C# program that copies the content of the bookmark (which is the table) and copy it several times after the the first table in the same document and replace the bookmarks contained in the table with some text. The original table in the bookmark should be deleted afterwards.

So basically this is what the document looks like structurally before the copying:
Static Text
Static Text
BookmarkStart(‘BookmarkName’)
Begin Table
Row with BookmarkStart (‘CreationDate’)
Text (will be replaced later)
Row with BookmarkEnd (‘CreationDate’)

     Row with BookmarkStart ('DeletionDate')
        Text (will be replaced later)
     Row with BookmarkEnd ('DeletionDate')
  End Table
BookmarkEnd('BookmarkName')

My program should convert it for example like that (2 copies):

Static Text
Static Text
  Begin Table
     Row with CreationDate1
     Row with DeletionDate1
  EndTable

  Begin Table
     Row with CreationDate2
     Row with DeletionDate2
  EndTable

I understand that the bookmarks’ names must be unique so therefore I suppose the bookmarks’ name could be changed by adding suffixes like “_1”, “_2” and so on.

Can you please explain to me how this can be done? I searched the forum for helpful articles but could only find very old ones but that didn’t help. Therefore any help is very much appreciated.

Regards,
Dirk.

@DIllenberger

In your case, we suggest you following solution.

  • Extract the content from document using bookmark. Please refer to the following article about extracting the content.
    Extract Content from a Bookmark
  • Move the cursor to the desired location in the document where you want to insert the content.
  • The extracted contents are in the form of Document object. Please create new bookmark and insert the document (extracted content) using DocumentBuilder.InsertDocument method.

To work with tables, please read the following article.
Working with Tables

Hope this helps you.