Inserting documents while performing Find and Replaces

Like the subject says, I want to parse a word Document and replace identified text with complete (but rather small) documents.

I declared a Document and a DocumentBuilder like:

Document doc;
DocumentBuilder builder;

doc = new Document("c:\\dest.doc");
builder = new DocumentBuilder(doc);

and I start the Find and Replace process with the following line:

doc.Range.Replace(new Regex("INSERT HERE"), new ReplaceEvaluator(repeval), true);

My ReplaceEvaluator function is freely inspired from the Append Document example provided by the help files and looks like:

private ReplaceAction repeval(object sender, ReplaceEvaluatorArgs e)
{
    builder.MoveTo(e.MatchNode);
    Document srcDoc = new Document("c:\\source.doc");
    srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
    foreach (Section srcSection in srcDoc)
    {
        Node dstSection = doc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        doc.AppendChild(dstSection);
        //doc.InsertAfter(dstSection, builder.CurrentNode);
    }
    e.Replacement = "";
}

It works well as long as the source documents are appended to the destination document. But what I would like to do is to perform an Insert instead of an Append and uncommenting doc.InsertAfter will always result in the error: “The reference node is not a child of this node.”.

Is there a way to achieve the desired result?

Thanks!

Hi,

Please consider the following article:

https://docs.aspose.com/words/net/insert-and-append-documents/,

the section Inserting the content of one document to another at different locations. It shows how to properly insert the whole document into an arbitrary location in the destination document. Hope this will eliminate the error. If not, just let me know.