Free Support Forum - aspose.com

How to replace text with table from other documents in aspose word c#

how to replace text with table from other documents in aspose word c#

FindReplaceOptions options2 = new FindReplaceOptions();
options2.Direction = FindReplaceDirection.Backward;
options2.ReplacingCallback = new ReplaceEvaluatorFindAndInsertTable();

wordDocument.Range.Replace(new Regex(“MileStone”), “”, options2);

import event :

private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
//Document subDoc = new Document(“mi.docx”);

            Document wordDocument = new Document("mi.docx");
            Table table1 = wordDocument.FirstSection.Body.Tables[0];
            //Table referenceTable = (Table)wordDocument.GetChild(NodeType.Table, 1, true);

            Document _rootDoc = args.MatchNode.Document as Document;

            Table importedTab = (Table)_rootDoc.ImportNode(table1, true);

            Node dstNode = _rootDoc.ImportNode(importedTab, true, ImportFormatMode.KeepSourceFormatting);

            //Node dstNode = _rootDoc.ImportNode(importedTab, true, ImportFormatMode.KeepSourceFormatting);
            // Insert a document after the paragraph, containing the match text
            Paragraph para = (Paragraph)args.MatchNode.ParentNode;

            para.AppendChild(dstNode);

            

            // Remove the paragraph with the match text
            para.Remove();

            return ReplaceAction.Skip;
        }
    }

i am getting below error:

Additional information: Cannot insert a node of this type at this location.

@Jana54,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document(s)
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.