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

Free Support Forum - aspose.com

Malformed document after importing another document

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)


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


Thanks for your request. Please try using ImportFormatMode.KeepSourceFormatting instead of ImportFormatMode.UseDestinationStyles. In this case the imported document looks as expected.

Hope this helps.

Best regards,

Hi Alexey,

The problem is not the layout, that is as expected. I even want the frament to conform itself to the styles defined in the template...

With a damaged result I really main a mallformed document:

When you set in the testcode the variable 'Fragment' to 'D:\MyFragment.docx' the generated Test.doc is fine;

but when you set in the testcode the variable 'Fragment' to 'D:\MyFragment.doc' the generated Test.doc will be mallformed. Word is opening it in the safe mode because 'there is a problem with the file'


Thanks for your request. I tried with both fragment documents on my side and both generated document are fine. MS Word 2007 can open them without any issues. Could you please attach your output documents here for testing?

Best regards,

See the attached files.

Both the fine version (created importing MyFragment.doc)

and the damaged version (created importing MyFragment.docx)


Thank you for additional information. But I can open both document on my side without any issues. I use MS Word 2007 for testing. Which version of MS Office do you use on your side?

Best regards,

Good morning,

I'm using Word 2010 with all the latest updates.

It does open the damaged file but shows a red bar with the text "Er is een probleem met dit bestand aangetroffen. Het bewerken van dit bestand kan schadelijk zijn voor uw computer. Klik hier voor meer informatie." freely translated this means: "There is as problem found in this file. Editing the file can be dangerous for your computer. Click here for more information"

The two files also differ in size. (That should be the same considering that the imported fragments where the same too besides their original format)

My customer is running Office XP and that one even crashes!


Thank you for reporting this problem to us. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

Best regards,

The issues you have found earlier (filed as WORDSNET-4964) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.