How to preserve formula field?

I use the following code when “finalizing” the document (before the result is saved to output file) just to make some custom field merging and to remove some garbage (unused merge fields and empty stuff):

document.MailMerge.FieldMerging += MailMerge_FieldMerging;
document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveUnusedFields |
    MailMergeClearOptions.RemoveEmptyTableRows |
    MailMergeClearOptions.RemoveEmptyRanges |
    MailMergeClearOptions.RemoveEmptyParagraphs;
document.MailMerge.Execute(new string[] { string.Empty }, string.Empty);

So far I was using GemBox 3.1 v1134 and everything was working fine.

Now I would like to upgrade to the latest version. The problem is that the formula field that was remaining before is being removed now. I noticed the support for formulas has been added in version 3.1 v1197.

How can I force the code in MailMerge_FieldMerging to not remove the formula but to clear everything else as before?

I already wrote a prototype. Just tell me in case it would help to attach it.

Thanks and regards
Lukas

Hi Lukas,

Currently, there is no option for this. However, as a workaround for now could you try converting FieldType.Formula fields to FieldType.Unknown so that the mail merge process doesn’t handle them.

For example:

foreach (var field in document.GetChildElements(true, ElementType.Field)
    .Cast<Field>()
    .Where(f => f.FieldType == FieldType.Formula)
    .Reverse())
{
    var cloneField = new Field(document, FieldType.Unknown,
        field.InstructionInlines.Select(i => i.Clone(true)),
        field.ResultInlines.Select(r => r.Clone(true)));

    field.Content.End.InsertRange(cloneField.Content);
    field.Content.Delete();
}

And then after the mail merge you would do the vice verse (change back the FieldType.Unknown fields to FieldType.Formula).

Does this work for you?

Regards,
Mario

Hi Mario,

yes, this workaround solves my problem. Thank you!

Best regards
Lukas