facebooktwittermenuarrow-up

GemBox Support Forum

Formula field instructions and nullables

Hi, I am curious if there is a better way to handle these scenarios besides wrapping them in IF fields:

  1. Using a nullable numeric inside a formula. If the value is null an exception will be thrown. Is there an easy way to treat null as default in this case?
  2. Using \b or \f instructions in a formula, like { = 5 / { MERGEFIELD Foo } \f " SF GBA" }. I understand Word doesn’t support these in formula fields - is there another recommended way to achieve this

Hi Kris,

  1. Is it a handled exception? That is expected if the formula is invalid and in that case, the calculation will not be performed.
    Nevertheless, can you try something like this:
var document = DocumentModel.Load("input.docx");

document.MailMerge.FieldMerging += (sender, e) =>
{
    bool isEmpty = !e.IsValueFound || e.Value == null;
    if (isEmpty)
    {
        Element parent = e.Field.Parent;
        while (parent.ElementType != ElementType.Document)
        {
            if (parent.ElementType == ElementType.Field && ((Field)parent).FieldType == FieldType.Formula)
            {
                e.Inline = new Run(e.Document, "0");
                break;
            }

            parent = parent.Parent;
        }
    }

};

document.MailMerge.Execute(new { Foo = (int?)null });

document.Save("output.docx");
  1. Unfortunately no, the calculation engine will take the whole instruction text of the formula field.
    These switches (\b and \f) could be a part of some valid formula (for instance, referring to bookmark names) so in that case, such behavior would not be desirable.
    You could perhaps handle this information in the FieldMerging event, but nevertheless, I believe that using the IF fields before or after the formula field would be a much simpler approach.

Regards,
Mario