Conditional formatting using Mail Merge

The combination of a table/list/database and the Word document seems super useful to me: to be able to generate, based on a template and a table, a series of documents that would contain many repetitive elements, such as invitations, diplomas,  certificates, etc. Working with the Mailings (Mail Merge) option from MS Word is not so difficult, being intuitive, but also supported by a small wizard.

My intention, in this article, is to exemplify the situation in which we would have to display numerical values (for example, grades), where either decimals appear or not. Thus, if it is justified (for example) to display 9.00, taking into account that after 9, in the Romanian scoring system, there is also the grade 10, when displaying the latter, it is no longer necessary to display it with decimals, not being able -he was waiting for us at a grade above 10. From this point of view, it would not be very elegant (in a perfectionist way) to display 10.00, as suggested in the image above.

For this aspect, you should follow some simple, but slightly delicate steps; this, because there are some small aspects that can lead to failure. First of all, it should be taken into account that working with special fields in Word involves certain rigors. For example, to make it seem a little mysterious: a brace inserted from the keys is not the same as the brace of the field (it can also be noticed visually but, obviously, mainly functionally).

Concretely, after we have created the grade field, if we press the key combination Alt+F9 (the most recommended way), we will notice that our document switches to the field code view mode, other than displaying the field name which is default in Mail Merge, when we create the template. In the case of our grade, we will mark the content as being of this manner: { MERGEFIELD Medie }. If we were strictly interested in displaying with two decimal places the numerical values in this field, then it would become like this: { MERGEFIELD Medie \# "0.00" }, however, in our case, as we mentioned, we are interested in the difference for the case where we are talking about grade 10. Thus, the IF function can be used, in the manner illustrated below:

{ IF { MERGEFIELD Medie } = "10" { MERGEFIELD Medie \# "0" } { MERGEFIELD Medie \# "0.00" } }

The insertion of the fields is done with the Ctrl+F9 key combination, otherwise it will not work if the braces are inserted from the keyboard.

If we want to speed things up, we can make the value red when the grade 10 is displayed, for example. This aspect can be done very simply by still being in code view mode and selecting the code that handles the differential display of 10 (ie the value_if_true component of the IF function), as in the example below:

{ IF { MERGEFIELD Medie } = "10" { MERGEFIELD Medie \# "0" } { MERGEFIELD Medie \# "0.00" } }.

  1. After any change you make in the code of a field, when returning to the results view mode (by pressing the combination Alt+F9 again) it is necessary to update that change, either by pressing the right mouse button on that field and choosing the Update Field option, from the context menu, either by selecting or placing on that field and pressing the F9 key.
  2. Unfortunately, if you want to try and Copy-Paste this code into a document, you’ll find it won’t work because, as I mentioned above, the braces are actually the active part of the code, not simple symbols entered by keys. You can convince yourself by being in such a field, where you enter braces from the keyboard, then press the key combination Ctrl+F9.
Synthesis of steps:

  • Ctrl+A – select the entire document;
  • Alt+F9 – switch fields/field in Code mode;
  • We select the content related to the related field, (e.g.:  MERGEFIELD  Medie) and press Ctrl+X;
  • between the braces left without content, we will have to insert the code:
    { IF {MERGEFIELD  Medie}="10" {MERGEFIELD  Medie\#"0"} {MERGEFIELD  Medie\#"0.00"} } 
    ATTENTION: braces are not inserted from the keyboard, but through the Ctrl+F9 combination, having a special functionality.
  • Ctrl+A (as a precaution) and pressing Alt+F9 to switch to normal mode;
  • F9 – the Refresh will be performed.

More examples:

  • Text type fields:
    • Written in uppercase (TEXT DE PROBĂ): { MERGEFIELD Luna\* Upper }
    • Written in lowercase (text de probă): { MERGEFIELD Luna\* Lower \* MERGEFORMAT }
    • First capital letter (Text de probă): { MERGEFIELD Luna\* FirstCap }
    • First letter of each word, capitalized (Text De Probă): { MERGEFIELD Luna\* Caps }
  • Date type fields.:
    • 5 mar. 2001: { MERGEFIELD DataNastere\@ "d MMM. yyyy" }
    • Friday, March 5, 2001: { MERGEFIELD DataNastere\@ "dddd, d MMMM yyyy" }
    • 05/03/2021: { MERGEFIELD DataNastere\@ "dd/MM/yyyy" }
    • 5-3-21: { MERGEFIELD DataNastere\@ "d-M-yy" }
  • Numeric fields.:
    • Display percentage: 44.44% { =100*{ MERGEFIELD Realiz_proc} \# "0.00%" }
    • Currency display: $120.25 {MERGEFIELD Valoarea \# $#,##0.00}

  1. yyyyyear; if you want to display the year in two digits: yy.
    M – moon; depending on how many M’s are placed, the month will be displayed with one digit (3), with two (03), with three letters (Mar.) or whole (March).
    d – day; just like the month, the display may be different.
  2. Keep in mind what the decimal delimitations are, respectively, the thousands separator, depending on the regional settings of the computer.

Author: Ovidiu.S

I am quite passionate about this professional area as if I know something - no matter how little - I want to share it with others.

Leave a Reply

Your email address will not be published. Required fields are marked *