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

Free Support Forum - aspose.com

Replace a merge Field with a table

Hi All ,

I would like to replace a merge field with a table created using Aspose Words.

Following are the steps which i would like to take place.

I will have a merge field called “table” .

I want to remove that merge field from the document and at the place of that merge field i wanted to replace a table ,which i created using Aspose Words.

Regards,
Jaswanth

Hi Jaswanth,

Thanks for your inquiry. You can simply move the cursor to target merge field and then insert Table. Please see the following articles:

https://docs.aspose.com/words/net/navigation-with-cursor/
https://docs.aspose.com/words/net/introduction-and-creating-tables/

I hope, this helps.

Best regards,

Hi ,

Thank You for the prompt Reply .
The problem now is ,I have a merge field ,which is repeated multiple times in the word document .

Like 3 to 4 times ,Now i need to insert the same table at all the places where there is a merge field .

I tried using MovetoMergeField , its just inserting at the first merge field location only ,ignoring the rest of the same merge fields .

Is there any other way ,i can the count of a specific merge field repeated in the document .

Thanks,
Jaswanth

Hi Jaswanth,

Thanks
for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.7.0) from here and let us know how it goes on your side. Please use the following code example to achieve your requirements. I have attached the input and output documents with this post for your kind reference.

Note that DocumentBuilder.MoveToMergeField method (String) moves the cursor to a position just beyond the specified merge field and removes the merge field.

Document doc = new Document(MyDir + "Test28.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("Test1");
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
// Build the second cell
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row.
builder.EndRow();
// Build the first cell of the second row.
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();
// Signal that we have finished building the table.
builder.EndTable();
builder.MoveToMergeField("Test1");
// We call this method to start building the table.
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1 Content.");
// Build the second cell
builder.InsertCell();
builder.Write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row.
builder.EndRow();
// Build the first cell of the second row.
builder.InsertCell();
builder.Write("Row 2, Cell 1 Content");
// Build the second cell.
builder.InsertCell();
builder.Write("Row 2, Cell 2 Content.");
builder.EndRow();
// Signal that we have finished building the table.
builder.EndTable();
doc.Save(MyDir + "Out.docx");

If you still face problem, please share your input and expected output document here for testing. I will investigate the issue and provide you more information.

What if i have 100 's Test1 merge field.

I should keep a for loop and keep looping .

Okay I am even fine with that.

But is there any way i can know ,the count of a certian merge field ,how many a time it is replicated in the document ?

So i can write down the for loop ? because i right now i dont know its count . Because document is generated of fly .

Jaswanth

Hi,

Thanks for your inquiry. You can determine how many times a particular merge field occurs in document by using the following code:

string[] fieldNames = doc.MailMerge.GetFieldNames();
var counts = fieldNames
    .GroupBy(w => w)
    .Select(g => new { fieldName = g.Key, Count = g.Count() })
    .ToList();
foreach (var str in counts)
    Console.WriteLine(str.fieldName + " -> " + str.Count);

I hope, this helps.

Best regards,