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

Free Support Forum - aspose.com

Error when copy the content of one document to another


when i try to copy the content of one document to another, using the following code:

While i < sectionCounter
Dim s As Section
s = src.Sections(i)
i += 1
End While

The following Arugument Exception occurs:

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

How can I fix this? And if there is an image in the content, will it be copied over like the text content? Or have to use alternative ways to copy it?

The document nodes are bound to the specific document. That’s why to copy nodes between documents you should use the Document.ImportNode method. Check the example here:


The use of Document.ImportNode only append one document to the end of another, but is there any way to insert the context of one document into another document at appointed locations e.g. at a bookmark.

Also when using ImportNode function, the combined document has a section break in between, is there any way to remove that section break? And the header and footers, how can I not copy across the header and footers.

Here is the sample code that inserts document after bookmarked paragraph:

private void InsertDocumentAtBookmark()
    // Open the document.
    string filename = Application.StartupPath + @"\Doc1.doc";
    Document doc = new Document(filename);
    InsertDocument(doc.Range.Bookmarks[“bokmarkName”].BookmarkStart, new Document(Application.StartupPath + @"\Doc2.doc"));
    // save resulting document with a new name
    string filenameOut = Application.StartupPath + @"\CombinedDoc.doc";

public void InsertDocument(Node node, Document doc)
    Document dstDoc = node.Document;
    Section insertedSection;
    int index;
    if (node.GetAncestor(typeof(Cell)) != null || node.GetAncestor(typeof(Shape)) != null)
        // Insertion point is tracked to Cell or Shape level:
        // - insert appended document on node by node basis.
        index = node.ParentNode.ChildNodes.IndexOf(node);
        foreach (Section section in doc.Sections)
            foreach (Node sourceNode in section.Body.ChildNodes)
                // Only Paragraph or Table nodes can be inserted into Cell or Shape
                if (sourceNode is Paragraph || sourceNode is Table)
                    // Do not insert node if it is a last empty paragarph in the section.
                    if (sourceNode is Paragraph && sourceNode == section.Body.LastChild && !sourceNode.HasInnerText)
                    Node insertedNode = dstDoc.ImportNode(sourceNode, true, ImportFormatMode.KeepSourceFormatting);
                    node.ParentNode.ChildNodes.Insert(index++, insertedNode);
        // Insertion point is tracked to Section.Body level:
        // - insert appended document on section by section basis.
        Section dstSection;
        Body body = (Body)node.GetAncestor(typeof(Body));
        if (body.LastChild != node)
            DocumentBuilder builder = new DocumentBuilder(dstDoc);
            dstSection = builder.CurrentParagraph.ParentSection;
            dstSection = (Section)node.GetAncestor(typeof(Section));
        index = dstDoc.Sections.IndexOf(dstSection);
        int index0 = index;
        foreach (Section section in doc.Sections)
            insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
            //Uncomment this line if you want to get rid of headers/footers in the inserted section
            dstDoc.Sections.Insert(index++, insertedSection);
        dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;

I have made InsertDocument procedure intentionally excessive so that it could be used in various document insertion scenarios. Note that it is featuring two approaches to document insertion - node-by-node and section-by-section. If you want to avoid inserting sections breaks to result document then you should import Section.Body immediate children one by one.

For a very simple scenarios when you just want to append contents of the section from another document you can use Section.AppendContent method to append contents of the inserted section to section already existing in the document. No section breaks will be added that way.

To get rid of headers/footers in the inserted section use ClearHeadersFooters method on it after it is imported.

Best regards,

When I try GetAncestor(typeof(cell)), it gives compilation error:

‘cell’ is a type and cannot be used as an expression.

how can i fix this?

In C# keyword typeof should be used with a class name not an instance name as a parameter. For example:

Node node = builder.CurrentNode.GetAncestor(typeof(Aspose.Words.Cell));

Best regards,

I am using VB.NET, but i think this shouldn’t be an issue. I tried to use Aspose.Words.Cell, but has the follwing compilation error:

‘Cell’ is a type in ‘Words’ and cannot be used as an expression

I’m also looking for a demostration or an explanation of how the nodes construct a word document.


In VB.NET TypeOf has a different meaning and should not be used in this context. Use GetType() instead:

Dim node As Node = doc.GetAncestor(GetType(Aspose.Words.Cell))

Best regards,


The GetType() worked. Thanks. I still have a question:

When the bookmark is within a Cell or run the first half of the code. A run time error is generated:

“Cannot insert a node of this type at this location.”

The error generated on the line:

node.ParentNode.ChildNodes.Insert(index++, insertedNode);

The node that is about to insert is a paragraph from another document. How can I fix this?

Please atach a test project together with the sample documents. I will check if the code can be updated to embrace the cases like this.

Hi Vladimir,

I put my code in a test project,with the two document that i’m trying to merge in the attachment. Can you please update the code, so that i don’t need to insert a sectionbreak around the attached document. Thank you for you help.



To fix the code add the following lines:

While n.NodeType <> NodeType.Paragraph

n = n.ParentNode

End While


If n.GetAncestor(GetType(Aspose.Words.Cell)) IsNot Nothing Or n.GetAncestor(GetType(Aspose.Words.Shape)) IsNot Nothing Then

and your project will work ok after this.

Sorry, have not understood the point about section breaks. Please clarify.

Best regards,


Thanks for the update. With the section breaks, what I wanted to do is to append the content of the second document right after (or right on) the bookmark position. i.e.


test content: [bookmarkstart][bookmarkend]

File 2:

inserted content

Combined File:

test content: [bookmarkstart]inserted content[bookmarkend]

The current code will insert the content within section breaks (looks like another paragraph). How can I append the content within a bookmark?



The method given above has actually two parts - one for inserting documents on node-by-node-basis and second for inserting documents on section-by-section basis. If you don’t want to insert section breaks in the resulting documents then you can remove the second part of the method and always go with the first.

Hope this helps,

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

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