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

Free Support Forum - aspose.com

How to insert and delete table after mail merge field using C#

I tried using bookmark in this way
Table table = (Table)doc.Range.Bookmarks[“xyz”].BookmarkStart.GetAncestor(NodeType.Table);
if (table != null)
{
table.Remove();
}

how to find Table as above with Fields and to get Ancestor using doc in aspose.

@saranyasrinivasan92

Could you please share your input and expected output documents here for our reference? We will then provide you more information about your query along with code.

Input document Details:

Step 1: Input doc as MailMerge.docx I have set text “xyz” as merge fields
string inputpath =“c://Data//MailMerge.docx”;
string outputpath =“c://Data//Out//MailMerge.docx”;

Step 2:

StartTag=“xyz”;
added one table after StartTag using ImportTableFromDataTable(builder,dataTable,true);
builder.MoveToMergeField(StartTag,true,false);
DataTable dtable = GetTable();
Table table = ImportTableFromDataTable(builder, dtable, true);

In GetTable()
Just adding one table with one row values.

save changes in document. check output 1 for expected output

Step 3: take the saved document and check for field having xyz - to get ancestor and find nodetype table
and to delete the table and will replace with another table.

using bookmark
Table table = (Table)doc.Range.Bookmarks[“xyz”].BookmarkStart.GetAncestor(NodeType.Table);
if (table != null)
{
table.Remove();
DataTable dtable = GetNewTable();
Table table = ImportTableFromDataTable(builder, dtable, true);
}

how to do with fields ?? to get expected output as output 2 as show below

Output 1:
<< xyz>>
table data

Output 2:
<< xyz>>
Newtable data

@saranyasrinivasan92

You can get the field name by FieldMergeField.FieldName. To get the ancestor table of mail merge field, please use field.Start.GetAncestor(NodeType.Table).

If the table is after mail merge field, you can use NextSibling property to get the table.

can you please share any sample code.

@saranyasrinivasan92

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

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we will analyze your test case and provide you code example. Thanks for your cooperation.

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

Create C# Console application and paste program.cs code

Check for comments
/* your logic here
how to delete existing table using fields
And replace with new table*/
//Delete table finding Solution.zip (82.2 KB)
xyz after table.

@saranyasrinivasan92

In your case, you need to move the cursor to the mail merge field and then get the next node of type Table. Please check the following modified code snippet. Hope this helps you.

else if (val == "2")
{
    inputpath = @"C:\temp\20.4.docx";
    outputpath = inputpath;
    string StartTag = "xyz";
    Document doc = new Document(inputpath);//Size 22k
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToMergeField(StartTag, true, false);

    Node node = builder.CurrentNode;
    node = node.NextPreOrder(doc);
    if(node.NodeType == NodeType.Table)
        node.Remove();
    /*your logic here 
    how to delete existing table using fields
    And replace with new table*/

    //Delete table finding tag xyz after table.

    //Inserting i can reuse same code.
    DataTable dtable = GetEmpsTable();
    Table table = ImportTableFromDataTable(builder, dtable, true);
    doc.Save(outputpath);
}

Thank you. In my case CurrentNode is getting null so i tried below code and working fine.

        Node node = builder.CurrentParagraph.NextSibling;
        builder.MoveTo(node);
        Node table = builder.CurrentParagraph.NextSibling;
        if (table.NodeType == NodeType.Table)
            table.Remove();

And I have one more doubt is that possible to Insert table in all findings of start tags. consider in input document having xyz duplicate two merge fields

<< xyz>>

<< xyz>>

how to insert in both place .currently in first finding place only table inserting.

for code , Please check for option 1 in same program.cs file

if (val == “1”)//First output-Insert Employee Table
{
string StartTag = “xyz”;
Document doc = new Document(inputpath);//Size 22k
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField(StartTag, true, false);
DataTable dtable = GetEmpTable();
Table table = ImportTableFromDataTable(builder, dtable, true);
doc.Save(outputpath);
}

@saranyasrinivasan92

In your case, we suggest you please remove the mail merge field from the document and replace it with simple text <<xyz>>. You can use find and replace feature to find the text and replace it with table or inert the table after <<xyz>>. Please read the following article.
Find and Replace

If you do not want to remove the mail merge field from the document, you can use following solution.

  1. Move the cursor before mail merge fields.
  2. Insert the bookmarks.
  3. Move the the cursor to bookmark and insert table as suggested in my previous post.

Hope this helps you.