Free Support Forum - aspose.com

Preserve Merge Feilds while ensuring Formula fields work

So we have some complicated word templates which include merge fields within formula fields and\or if fields.
e.g.

{= {MERGEFIELD Number1} * {MERGEFIELD Number2}}

When running a regular mail merge, this works as expected.
However, we need to preserve the mail merge fields after a mail merge.
We are currently using this code:

public class FieldMergingCallback : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue == null)
        {
            return;
        }
        var builder = new DocumentBuilder(e.Document);

        var fieldCode = e.Field.GetFieldCode();
        var fieldValue = e.FieldValue;
        var fieldValueString = fieldValue.ToString();

        if (fieldCode.Contains("\\") && !fieldCode.Contains("MERGEFORMAT"))
        {
            var format = fieldCode.Substring(fieldCode.IndexOf("\\") + 1).Replace("@", "").Replace("\"", "").Trim();
            if (format != "")
            {
                if (fieldValue is DateTime)
                {
                    var date = (DateTime) fieldValue;
                    fieldValueString = date.ToString(format);
                }
                else if (fieldValue is long)
                {
                    var l = (long) fieldValue;
                    fieldValueString = l.ToString(format);
                }
                else if (fieldValue is decimal)
                {
                    var m = (decimal) fieldValue;
                    fieldValueString = m.ToString(format);
                }
            }
        }

        builder.MoveTo(e.Field.End);
        builder.InsertField(fieldCode, fieldValueString);

        e.Text = "";
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e) { }
}

When I run this code, I do get standard merge fields retained however nested fields are not replaced, and values are not merged

I have also tried with the following:

public class FieldMergingCallback : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue == null)
        {
            return;
        }
        var builder = new DocumentBuilder(e.Document);

        var fieldCode = e.Field.GetFieldCode();
        var fieldValue = e.FieldValue;
        var fieldValueString = fieldValue.ToString();
 
        if (fieldCode.Contains("\\") && !fieldCode.Contains("MERGEFORMAT"))
        {
            var format = fieldCode.Substring(fieldCode.IndexOf("\\") + 1).Replace("@", "").Replace("\"", "").Trim();
            if (format != "")
            {
                if (fieldValue is DateTime)
                {
                    var date = (DateTime) fieldValue;
                    fieldValueString = date.ToString(format);
                }
                else if (fieldValue is long)
                {
                    var l = (long) fieldValue;
                    fieldValueString = l.ToString(format);
                }
                else if (fieldValue is decimal)
                {
                    var m = (decimal) fieldValue;
                    fieldValueString = m.ToString(format);
                }
            }
        }

        builder.MoveToMergeField(e.FieldName);
        builder.InsertField(fieldCode, fieldValueString);

        e.Text = "";
    }

   void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
   {
       Console.WriteLine(e.ImageFileName);
   }
}

This code has greater success fields get replaced both nested and otherwise. However, it only replaces the first instance of the merge field, and subsequent merge fields are not replaced.

@Mark-Broadhurst,

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.