Free Support Forum -

Error in ExtractBetweenParagraph .NET Example

We are running the exact sample code in ExtractNodesBetweenParagraph and it fails with the following message.

This did work on the sample doc, but none of our test documents. Even a brand new, basic doc in Word 2010. We’ve set the parameters to do paragraphs 1-2, and the exact sample, with more than enough paragraphs. Thanks in advance.

System.ArgumentException: ‘Start node and end node must be a child or descendant of a body’


Thanks for your inquiry. Please make sure that you are passing Paragraph nodes to extract content method. If you still face problem, please share following resources here for testing.

  • Your input Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Thanks for the quick response. I"m running the exact example from the repo

With a different directory and file. I’ve included the file.


Thanks for your inquiry. The FirstSection.GetChild method gets the child node from header/footer and body of section. Please use FirstSection.Body.GetChild instead as show below. We will fix the code example at Github repository.

Document doc = new Document(MyDir + "samplepractice_notoc.docx");

// Gather the nodes. The GetChild method uses 0-based index
Paragraph startPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 6, true);
Paragraph endPara = (Paragraph)doc.FirstSection.Body.GetChild(NodeType.Paragraph, 10, true);
// Extract the content between these nodes in the document. Include these markers in the extraction.
ArrayList extractedNodes = Common.ExtractContent(startPara, endPara, true);

// Insert the content into a new separate document and save it to disk.
Document dstDoc = Common.GenerateDocument(doc, extractedNodes);

dstDoc.Save(MyDir + "18.6.docx");

Works like a champ!!!