"The newChild was created from a different document than the one that created this node."

Hey, im having a problem with importing a table and mergefields within a bookmark from one document and placing it at a specified mergefield within another document. I get this error “The newChild was created from a different document than the one that created this node.”

The code i have is as follows:

Bookmark BK = doc.Range.Bookmarks["Bookmark1"];
ArrayList extractedNodesExclusive = ExtractContent(BK.BookmarkStart, BK.BookmarkEnd, false);
for (int i = 0; i < extractedNodesExclusive.Count; i++)
{
    builderMainTemp.InsertNode((Node)extractedNodesExclusive[i]);
}

I am using the methods provided here:
https://docs.aspose.com/words/net/programming-with-documents/

As i understand it i have to use NodeImporter because a node relies on its parent nodes for styles, but this does not allow me to insert at a specified location within a document, is this possible? or do i have to import the whole document into a specified location (i have already achieved this). It would just be easier to have a single document with bookmarks specifying different regions rather than lots of documents with small amounts of information in them. Thanks in advance

Hi Fabian,

Thanks for your inquiry. In your case, I suggest you please use the NodeImporter.importNode method to import a node from one document into another. NodeImporter Class allows to efficiently perform repeated import of nodes from one document to another. Aspose.Words provides functionality for easy copying and moving fragments between Microsoft Word documents. This is known as “importing nodes”. Before you can insert a fragment from one document into another, you need to “import” it. Importing creates a deep clone of the original node, ready to be inserted into the destination document. The simplest way to import a node is to use the ImportNode method provided by the DocumentBase object.

You are extracting contents from one document and inserting it into another document. Please use the following code snippet to achieve your requirement. Please check the detail of InsertDocument and GenerateDocument from here:

https://docs.aspose.com/words/net/insert-and-append-documents/
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

Following code example extract the contents of a bookmark from one document and insert extracted contents into another document at the position of Mail Merge field. Hope this helps you. Please let us know if you have any more queries.

Document srcDoc = new Document(MyDir + “src.docx”);
Bookmark BK = srcDoc.Range.Bookmarks["Bookmark1"];
ArrayList extractedNodesExclusive = ExtractContent(BK.BookmarkStart, BK.BookmarkEnd, true);
Document tmpDoc = GenerateDocument(srcDoc, extractedNodesExclusive);
Document dstDoc = new Document(MyDir + "dst.docx");
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveToMergeField("MailMerge");
Paragraph para = builder.CurrentParagraph;
InsertDocument(para, tmpDoc);
dstDoc.Save(MyDir + "Out.docx");

Thanks alot


Quick response and exactly what i was looking for awesome! :slight_smile:

Hi Fabian,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.