0

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.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
David
  • 214
  • 3
  • 15
  • Hmmm, three questions and no code is "broad" for Stack Overflow... It's also not completely clear what it is you want to ask - you start off with one thing, then digress into another. Could you please trim this down to a single question (and if you then have further questions, start new topics). – Cindy Meister Aug 20 '18 at 13:42
  • To your first/main topic: If you're asking about the \* MergeFormat switch in a field code, this switch tells the field to retain formatting applied *to the field result* when the field is updated. The formatting is "remembered" *per character* which can have rather odd results. The general recommendation by Word professionals is that this switch should be *avoided* unless you know you want that result. `\* CharFormat` on the other hand can be very useful as it forces a field to adpat the formatting applied to the first character in the field. – Cindy Meister Aug 20 '18 at 13:45
  • @CindyMeister, Thanks, and sorry about no code. I wasn't really sure what code to put, as this is more a question about the OpenXML SDK and if there are any features to help me. For example, if I want to make a date in a particular format, I would .ToString("dd MMM yyyy") but that doesn't really answer the question as to whether the SDK has something to format it. My biggest issue is about formatting the numbers... I can't do something like... MyNumerString.ToString("#,##0.00%") like I can with dates. – David Aug 20 '18 at 17:14
  • For advice on a wide range of field formatting and related issues, see the **Mailmerge Tips and Tricks** thread at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html or: http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks. I can't see that the MERGEFORMAT switch has anything to contribute. – macropod Aug 21 '18 at 03:48

2 Answers2

1

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.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
  • Thank you. I have the inserting of rows in a table working. It took a little bit of thinking about how to do that. Basically, I am gathering all tables, with a particular merge element name, then copy the row (as a template), fill it, insert it and after all done, delete the template row. The others like INCLUDEPICTURE etc. look interesting, so I will have a look at that. – David Aug 21 '18 at 11:09
  • Just as an FYI, the last ones I remember are [MERGEBARCODE](https://msdn.microsoft.com/en-us/library/hh745903(v=office.12).aspx) and [DISPLAYBARCODE](https://msdn.microsoft.com/en-us/library/hh745901(v=office.12).aspx) fields. – Mario Z Aug 21 '18 at 13:34
0

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.

David
  • 214
  • 3
  • 15