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)
    <span style="color:blue;">Dim</span> para <span style="color:blue;">As</span> <span style="color:#2b91af;">Paragraph</span>
    <span style="color:green;">' We will be inserting into the parent of the destination paragraph.</span>
    <span style="color:blue;">Dim</span> dstStory <span style="color:blue;">As</span> <span style="color:#2b91af;">CompositeNode</span> = insertAfterNode.ParentNode

    <span style="color:green;">' This object will be translating styles and lists during the import.</span>
    <span style="color:blue;">Dim</span> importer <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">NodeImporter</span>(srcDoc, insertAfterNode.Document, <span style="color:#2b91af;">ImportFormatMode</span>.KeepSourceFormatting)
    <span style="color:blue;">Dim</span> isFirstPara <span style="color:blue;">As</span> <span style="color:blue;">Boolean</span> = <span style="color:blue;">True</span>

    <span style="color:green;">' Loop through all sections in the source document.</span>
    <span style="color:blue;">For</span> <span style="color:blue;">Each</span> srcSection <span style="color:blue;">As</span> <span style="color:#2b91af;">Section</span> <span style="color:blue;">In</span> srcDoc.Sections
        <span style="color:green;">' Loop through all block level nodes (paragraphs and tables) in the body of the section.</span>
        <span style="color:blue;">For</span> <span style="color:blue;">Each</span> srcNode <span style="color:blue;">As</span> <span style="color:#2b91af;">Node</span> <span style="color:blue;">In</span> srcSection.Body
            <span style="color:green;">' Let's skip the node if it is a last empty paragraph in a section.</span>
            <span style="color:blue;">If</span> srcNode.NodeType.Equals(<span style="color:#2b91af;">NodeType</span>.Paragraph) <span style="color:blue;">Then</span>
                para = <span style="color:blue;">CType</span>(srcNode, <span style="color:#2b91af;">Paragraph</span>)

                <span style="color:blue;">If</span> insertAfterNode.NodeType = <span style="color:#2b91af;">NodeType</span>.Table <span style="color:blue;">Then</span>
                    <span style="color:green;">' :to do</span>
                <span style="color:blue;">ElseIf</span> insertAfterNode.NodeType = <span style="color:#2b91af;">NodeType</span>.Paragraph <span style="color:blue;">Then</span>
                    <span style="color:blue;">If</span> <span style="color:blue;">DirectCast</span>(insertAfterNode, <span style="color:#2b91af;">Paragraph</span>).IsEndOfSection <span style="color:blue;">AndAlso</span> (<span style="color:blue;">Not</span> para.HasChildNodes) <span style="color:blue;">Then</span>
                        isFirstPara = <span style="color:blue;">False</span>
                        <span style="color:blue;">Continue For</span>
                    <span style="color:blue;">End</span> <span style="color:blue;">If</span>
                <span style="color:blue;">End</span> <span style="color:blue;">If</span>
            <span style="color:blue;">End</span> <span style="color:blue;">If</span>

            <span style="color:green;">' This creates a clone of the node, suitable for insertion into the destination document.</span>
            <span style="color:blue;">Dim</span> newNode <span style="color:blue;">As</span> <span style="color:#2b91af;">Node</span> = importer.ImportNode(srcNode, <span style="color:blue;">True</span>)

            <span style="color:green;">' Insert new node after the reference node.</span>
            dstStory.InsertAfter(newNode, insertAfterNode)

            insertAfterNode = newNode
        <span style="color:blue;">Next</span> srcNode
    <span style="color:blue;">Next</span> srcSection
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>

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")