DocumentBuilder.MoveToMergeField function bug!

Hi,
In my program, I use merge field to replace data. Then after merging, I want to replace all the remaining merge field with a custom error field.
The code is as bellow:

DocumentBuilder builder = new DocumentBuilder(doc);
foreach (String field in doc.MailMerge.GetFieldNames())
{
    builder.MoveToMergeField(field);
    builder.InsertField(@"MERGEFIELD ERROR! * MERGEFORMAT", "ERROR!");
}

It works fine with all normal field, but with the “TableStart:xxx” and “TableEnd:xxx” fields, the “ERROR!” field was inserted but, the “TableStart” and “TableEnd” fields were still there.
Can you suggest any solution to manually delete these fields?
Thanks.

Hi
Thanks for your inquiry. I think that you can try using the following code example.

Document doc = new Document(@"Test012\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
string[] names = doc.MailMerge.GetFieldNames();
for (int i = 0; i < names.Length; i++)
{
    if (names[i].Contains("TableStart:"))
        names[i] = names[i].Replace("TableStart:", "");
    if (names[i].Contains("TableEnd:"))
        names[i] = names[i].Replace("TableEnd:", "");
    builder.MoveToMergeField(names[i]);
    builder.InsertField(@"MERGEFIELD ERROR! \* MERGEFORMAT", "ERROR!");
}
doc.Save(@"Test012\out.doc");

I hope this could help you.
Best regards.

Thank you for your quick response.
This solved my problem but there is another problem:
If there is only one invalid merge region element (TableStart or TableEnd), the MailMerge.ExecuteWithRegions method always throw an exception, even when the input data presents data of a valid merge region.
If I want to replace only invalid merge region fields, is there any way to do it, or I must manually remove these invalid fields?
Thanks in advance.

Hi
Thanks for your inquiry. I think that there can’t be only one invalid merge region element. Merge region should start with TableStart and finish with TableEnd. Could you please tell me how you determine invalid MERGEFIELD?
Best regards.