Using shortcuts for string formatting

We use this to format a date in the merged field {{NoticeDate\@ "MM/dd/yyyy"}} on the MS Word template.

Wanted to know if there is such a shortcut to format the incoming string (EM9879999222) in a particular pattern as EM-987-9999-222 etc. Thanks.

@WCBP4DocGenUser

Summary
Word merge fields only have built‑in switches for dates (@) and numbers (#). There is no switch that can re‑format an arbitrary string such as EM9879999222 into EM‑987‑9999‑222 directly in the field code.

Why it happens
The {{NoticeDate\@ "MM/dd/yyyy"}} syntax works because the \@ switch is defined for date values. For plain‑text MERGEFIELDs the supported switches are limited (e.g., \* Upper, \* Lower). A generic “string‑format” switch does not exist, so the desired pattern cannot be expressed with field switches alone.

What to do next

  1. Format the value before the merge – generate the correctly‑formatted string in your data source (e.g., EM-987-9999-222) and insert it with a simple merge field like {{NoticeNumber}}.
  2. Apply custom formatting during the merge – use a mail‑merge callback or similar programming hook in your automation code to transform the raw value into the required pattern before it is written to the document.
  3. Post‑process the document – after the merge, run a find‑and‑replace operation that looks for the raw pattern and inserts the hyphens.

Choose the approach that best fits your workflow. All of them involve handling the string in code or in the data source, because Word’s field switches cannot perform this type of string manipulation.

Note: The answer is based on general Word merge‑field behavior; specific SDK details were not found in the provided evidence.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

@WCBP4DocGenUser The only way, I see, to achieve this is using IFieldMergingCallback. For example see the attached template and code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.FieldMergingCallback = new CustomFormatter();
doc.MailMerge.Execute(new string[] { "Test" }, new string[] { "EM9879999222" });
doc.Save(@"C:\Temp\out.docx");
private class CustomFormatter : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        Field f = args.Field;
        string value = args.FieldValue.ToString();
        Regex r = new Regex(@"([A-Z]{2})(\d{3})(\d{4})(\d{3})");
        if (f.Format.NumericFormat == "CUSTOM" && r.IsMatch(value))
        {
            Match m = r.Match(value);
            args.Text = $"{m.Groups[1].Value}-{m.Groups[2].Value}-{m.Groups[3].Value}-{m.Groups[4].Value}";
        }
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing
    }
}

in.docx (13.9 KB)
out.docx (11.2 KB)