Free Support Forum -

Insert Word Document at Merge Field without Section Breaks

Does anyone have an example of how to insert a Word document into another Word document at a MergeField location without adding extra section breaks.

The only example I have seen for inserting a document involved the ImportNode() & Document.AppendChild() methods. Unfortunately I need to be able to insert the file at a specific point in the document as well as I don't want to have all these section breaks in my final document which seemed to get created when you copy the section nodes.

I have attempted to copy all the child nodes from the first section of the document being inserted but it seems like I might be at this for a long time.

Public Sub Aspose_InsertFile(ByVal oDoc As Document,
ByVal Path As String,
ByVal oMergeNode As CompositeNode)

Dim fFirstSection As Boolean = True

If Not IO.File.Exists(Path) Then Exit Sub

Dim oInsertDoc As New Document(Path)

' For a twist, let's say I want the second document to start on the same page where
' the first document ends. This is controlled by a property of the first section.
oInsertDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous

' Loop through all sections in the source document.
' Section nodes are immediate children of the Document node so we can just enumerate the Document.

For Each oInsertSection As Section In oInsertDoc


'The First Section should be copied in wihtout any Section Break ... so We copy the paragraphs and range

If fFirstSection Then

Dim oInsertBody As Body = oDoc.ImportNode(oInsertSection.Body, True, ImportFormatMode.KeepSourceFormatting)

For Each oNode As Node In oInsertBody.GetChildNodes(NodeType.Any, False, False)

oMergeNode = oMergeNode.ParentNode.InsertAfter(oNode, oMergeNode)


fFirstSection = False


' Because we are copying a section from one document to another,
' it is required to import the Section node into the destination document.
' This adjusts any document-specific references to styles, lists, etc.
' Importing a node creates a copy of the original node, but the copy
' is ready to be inserted into the destination document.

Dim oInsertSectionNode As Node = oDoc.ImportNode(oInsertSection, True, ImportFormatMode.KeepSourceFormatting)

' Now the new section node can be appended to the destination document.
oDoc.AppendChild(oInsertSectionNode) ', oMergeNode )

End If


End Sub

Please check the following article in our Wiki that covers this topic:

I have just updated this article with some new info on merging documents during the MailMerge.

Please let me know if you find it helpful. Your feedback is highly appreciated.

Best regards,

Thanks ... it looks like it should do everything I need. I will let you know if it doesn't.

By the way, Aspose support is way better than TXTextControl !

Just to let you kow, they have a comparison of the speed of their control against the Aspose Word / PDF combo, but their test app probably didn't take advantage of the newer DocBuilder.MoveToMergeField(Field) method as well as they completely ignore the fact that you guys support way more MS Word features and formatting than they do.

Thank you for such a high appraisal of our efforts!

Speaking of comparison results published by our competitor, they were obsolete already by the time they were published. They were relatively close to truth for October 2005. But I have reproduced this test in December 2005 and the speed in the setup they offered (mailmerge, then PDF conversion) was approximately equal on complex documents, having tables graphics etc. And their PDF conversion was of noticeably lesser quality. And in scenarios not involving PDF conversion our speed is way better. That's why they have never updated the test results or published more tests.

In general it is entirely incorrect for the producer to publish test results that prove that their component is better than of their competitor's. Such test can only be conducted and published by independent parties. Imagine one of the candidate for president elections officially conducting and publishing exit polls or maintain vote counting. And if you publish such a test you should at least make all of the test setup public, so that anybody could conduct and reproduce the test results.