David
David

Reputation: 366

C# OpenXML Mail Merge mergeformat

I am using OpenXML to handle a mailmerge. I am feeding in data from a json file to merge with my document.

I have this working great, but then I need to take in the formatting that is described in the MERGEFORMAT, as it doesn't look like OpenXML is handling this for me.

I have coded for the CAPS, FirstCap, Upper and Lower. Also coded (not tested yet) for the date and time formatting (starts with \@ ) and also managed \f and \b. (Not sure what to do about \m or \v.

I am now looking at doing the numbers, but while I see a number starts with #, I am not really sure how to apply the number options in code.

Firstly, am I going about this correctly (applying all this in code) or is there something I am missing that I can use in the SDK?

Secondly, how do I work with the numbers?

Thirdly, are there any merge options that I am missing?

Thanks.

Upvotes: 0

Views: 2212

Answers (2)

Mario Z
Mario Z

Reputation: 4381

OpenXML SDK does not provide something like this, but nevertheless essentially what you need is exactly what you mentioned. You will first need to parse your data as Double or DateTime and then call ToString on them by passing the format parameter that is specified in the MergeField.

However just in case you're interested, the OpenXML SDK PowerTools has a DocumentAssembler module that does a similar thing, it generates a resulting document by combining a template document and a data source. But in this case the template document has Content Controls or just a custom text placeholders with specific syntax (instead of MergeFields) and the data is provided in XML format (instead of JSON).

Nevertheless, if you still want to leverage the mail merge options then you have pretty much covered all the switches that are available in MergeFields.
The only thing that's left is to add a support for more fields that are related to mail merging, depending on your exact requirement (like INCLUDEPICTURE, INCLUDETEXT, MERGESEQ, MERGEREC, NEXT etc.).

Also, a support for some form of mail merge grouping would be rather beneficial. With this you would be able to merge multiple records in some merge range.
For instance, let's say you define a content that should be repeated and filled out based on your records, like a single table row which has some MergeFields. Then with a support for this feature you would be able to dynamically generate new row for each item in some JSON array and also each row would have appropriate data from its item.

I hope this gives you some ideas.

Upvotes: 1

David
David

Reputation: 366

After my comment... I did wonder if the string.ToString() was overloaded, so did further looking.

While a string.ToString() doesn't have the overload, the double does... so, convert the numeric string to a double, then .ToString that with the number format required.

https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

Thanks for looking.

Upvotes: 0

Related Questions