Free Support Forum - aspose.com

Replacing missing mail merge fields with static text

Can anyone suggest a simple and elegant way to replace all missing mail merge fields with static text?

I do not require any logic to the text or the mail merge field. Quite simply, the scenario is:

if mailmergefield.ismissing then

replace mailmergefield.text with "SORRY DATA IS MISSING"

Hi David,


Thanks for your inquiry. One simple way to achieve this as follows:

Document doc = new
Document(@“C:\Temp\a.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

doc.MailMerge.Execute(new string[] { "a", "d" }, new object[] { 1, 4 });

string[] remainingMergeFields = doc.MailMerge.GetFieldNames();

foreach (string fieldName in remainingMergeFields)

{

builder.MoveToMergeField(fieldName);

builder.Write("SORRY DATA IS MISSING");

}

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best Regards,