Error:Cannot insert a node of this type at this location

Hi,
I am trying to insert a table from the document called “BookMarkDocument”(Please find attached) at a bookmark called “Test” in a document called “MainDocument”(Please Find attached).I used the method “InsertDocument” exactly as mentioned in
https://docs.aspose.com/words/java/insert-and-append-documents/
If I do that, I am getting an error saying “Cannot insert a node of this type at this location” at the following line of code
dstStory.insertAfter(newNode, insertAfterNode);
Kindly, let me know how I can resolve this issue
Regards,
Pavithra V.S

Hi

Thanks for your request. I used the following code for testing and all works fine on my side:

// Open documents
Document doc1 = new Document(@"Test179\MainDocument.doc");
Document doc2 = new Document(@"Test179\Bookmark+Document.doc");
// Get bookmark
Bookmark bk = doc1.Range.Bookmarks["Test"];
if (bk != null)
{
    // Get parent paragraph of hte bookmark
    Node insertAfter = bk.BookmarkEnd.GetAncestor(NodeType.Paragraph);
    // Insert another docuemnt into the main document
    InsertDocument(insertAfter, doc2);
}
// Save output document
doc1.Save(@"Test179\out.doc");

Also, since you need to insert another document at the end of the main document, maybe you can use Document.AppendDocument method:
https://docs.aspose.com/words/java/insert-and-append-documents/
Best regards.

Hi,
I tried it out the way you said.It still gives me the same error at the same place in the InsertAfter method.I am not able to find out where is the document.

I see you use Java. Here is the same code in Java:

// Open documents
Document doc1 = new Document("C:\\Temp\\MainDocument.doc");
Document doc2 = new Document("C:\\Temp\\Bookmark+Document.doc");
// Get bookmark
Bookmark bk = doc1.getRange().getBookmarks().get("Test");
if (bk != null)
{
    // Get parent paragraph of hte bookmark
    Node insertAfter = bk.getBookmarkEnd().getAncestor(NodeType.PARAGRAPH);
    // Insert another docuemnt into the main document
    InsertDocument(insertAfter, doc2);
}
// Save output document
doc1.save("C:\\Temp\\out.doc"); 

And still this code works fine on my side. Which version of Aspose.Words do you use? I use the latest version of Aspose.Words for Java (3.0.1). you can download it from here:
https://releases.aspose.com/words/net
Also, please make sure you did not change anything in InsertDocument method.
Best regards.

Hi,
I am using a licensed version of Aspos.words(2.2).
I am still getting the same old error.It would be great if you can provide us the lsit of possibilities for the occurrence of this error.
Regards,
Pavithra V.S

Hi,
I am using a licensed version of Aspose.words(2.2).
I am still getting the same old error.It would be great if you can provide us the lsit of possibilities for the occurrence of this error.
Regards,
Pavithra V.S

Hi

Thank you for additional information. It is difficult to tell what causes this problem. Your license provides one year subscription for free upgrades to new versions released during this time. So, please check the expiration date of your license and upgrade to the newest version. To check an expiration date of your license open the license file using notepad. You will see the following tags:
20090218
It means that you can free upgrade to version of Aspose.Words published before 02/18/2009.
Best regards.

Hi,
I upgraded the version of Aspose.words.No progress, I am getting the same error.I tried doing it with inserting a document using a mailmerge field as well.It is also giving the same error.Can you suggest me anything else to get the implementation of inserting a table from document ‘X’ in the middle of document ‘Y’

Hi Alexey,
Thank you for your constant support! It worked.The problem was in changing the enhanced for loop to jdk 1.4 compatible format. We use jdk 1.4
I got to know about it in the following post.
getChildNodes()
It worked propelry after I changed it.https://forum.aspose.com/t/91060
Thanks Again for the support!
Regards,
Pavithra V.S

It is perfect that you already resolved the problem on your side. Please let me know in case of any issues.