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
oInsertSection.ClearHeadersFooters()
'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)
Next
fFirstSection = False
Else
’ 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
Next
End Sub