I have encountered a weird bug (using Aspose.Words for .Net version 10.1):
I have a document that I created bij opening it from a .dot file. Then I import on a certain location marked with a bookmark a second document using the InsertDocument function from the documentation. If that second document was created by opening a .doc file the final document is damaged, if the second document was created by opening a .docx file its fine… (both files are the same besides their format).
The same routine is used for other documents without any problem.
Here is my testing code: (Just create a new console app, reference Aspose.Words and paste the code): Use the files from the enclosed zip as the source document by placing then on D:\ (or change the testcode)
Imports Aspose.Words Module Module1 Const Template = "D:\MyTemplate.dot" Const Fragment = "D:\MyFragment.doc" 'Change to docx for working version Const Output = "D:\Test.doc" Const BookmarkName = "VolgendePagina" Sub Main() Dim MainDoc As New Document(Template) Dim SubDoc As New Document(Fragment) Dim BookMark As Bookmark = MainDoc.Range.Bookmarks.Item(BookmarkName) InsertDocument(BookMark.BookmarkStart.ParentNode, SubDoc) MainDoc.Save(Output) End Sub Private Sub InsertDocument(ByVal insertAfterNode As Node, ByVal srcDoc As Document) ' Make sure that the node is either a pargraph or table. If ((Not insertAfterNode.NodeType.Equals(NodeType.Paragraph))) And ((Not insertAfterNode.NodeType.Equals(NodeType.Table))) Then Throw New ArgumentException("The destination node should be either a paragraph or table.") End If ' 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.UseDestinationStyles) ' 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 ' 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 End Module