We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert document at a bookmark inside a text


I have a cell that contains the following text:
test part1 test part2 test part3

I putted the bookmark b1 around the text “test part2”.

How can I insert a document at the bookmark b1 in order to replace the old value of the bookmark b1 (the old value is “test part2”) by a new value. And at the same time keeping the text “text part1” and “test part3” at their correct location?

N.B.: I’m obliged to use the insert document. I cannot use the replacement method.


Hi Sara,

Thanks for your inquiry. In your case, we suggest you please move the cursor to the bookmark and insert the document using DocumentBuilder.InsertDocument method. Please use following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark bm = doc.Range.Bookmarks["b1"];
bm.Text = "";
Document doc2 = new Document(MyDir + "in2.docx");
builder.InsertDocument(doc2, ImportFormatMode.KeepSourceFormatting);
doc.Save(MyDir + "Out.docx");

Hi Tahir,

Thanks for you quick reply.

Currently, I’m using the version of Aspose.Words
This version doesn’t support the function builder.InsertDocument and I’m obliged to keep it.

The function that I’m using to insert a document at a specific bookmark is the following:

Private Sub InsertDocument(ByVal insertAfterNode As Node, ByVal srcDoc As Document)
    Dim para As Paragraph
    ' We will be inserting into the parent of the destination paragraph.
    Dim dstStory As CompositeNode = insertAfterNode.ParentNode

    ' This object will be translating styles and lists during the import.
    Dim importer As New NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting)
    Dim isFirstPara As Boolean = True

    ' Loop through all sections in the source document.
    For Each srcSection As Section In srcDoc.Sections
        ' Loop through all block level nodes (paragraphs and tables) in the body of the section.
        For Each srcNode As Node In srcSection.Body
            ' Let's skip the node if it is a last empty paragraph in a section.
            If srcNode.NodeType.Equals(NodeType.Paragraph) Then
                para = CType(srcNode, Paragraph)

                If insertAfterNode.NodeType = NodeType.Table Then
                    ' :to do
                ElseIf insertAfterNode.NodeType = NodeType.Paragraph Then
                    If DirectCast(insertAfterNode, Paragraph).IsEndOfSection AndAlso (Not para.HasChildNodes) Then
                        isFirstPara = False
                        Continue For
                    End If
                End If
            End If

            ' This creates a clone of the node, suitable for insertion into the destination document.
            Dim newNode As Node = importer.ImportNode(srcNode, True)

            ' Insert new node after the reference node.
            dstStory.InsertAfter(newNode, insertAfterNode)

            insertAfterNode = newNode
        Next srcNode
    Next srcSection
End Sub

After using this function, the new value of the bookmark b1 is putted after the text “text part3” (at the end of the cell) instead of putting it in the correct location (between the text “test part1” and the text “test part3”).
I think that the line code: “dstStory.InsertAfter(newNode, insertAfterNode)” is causing this behavior.
Do you have any suggestions to fix this issue?


Hi Sara,

Thanks for your inquiry. In your case, we suggest you following solution.

  1. Please move the cursor to the bookmark.
  2. Insert paragraph break.
  3. call InsertDocument as shown below.

Hope this helps you.

Dim doc As New Document(MyDir + "in.docx")
Dim builder As New DocumentBuilder(doc)
Dim bm As Bookmark = doc.Range.Bookmarks("b1")
bm.Text = ""
Dim doc2 As New Document(MyDir + "in2.docx")
InsertDocument(bm.BookmarkStart.ParentNode, doc2)
doc.Save(MyDir + "Out.docx")