How to change merge field color based on value of that merge field?

Hi,
I have a mail merge document. There is a mailmerge field <<year>>. Depending on it's value, we want to print the year in different font colors.
For instance if year = 2013, then font color = black,
if year = 2014 then font color = green,
 if year = 2015 then font color = blue.
I have reviewed  this example: {IF{MERGEFIELD Addr1} = "PO *" {MERGEFIELD Addr1 \* Upper} {MERGEFIELD Addr1 \*Lower \*Caps}}
where 'Addr1' is the data field's name.   But the switches do not seem to include the font color.
I have thought about macro code but am not proficient in VB code and cannot find an example where they are changing the color based on the value of the field.
It seems like a simple task, but I have spent 5 hours trying to find something to guide me and now submit my question for your help.
Thank you in advance for your assistance.
Best Regards,
KClench

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

There are various ways of doing this, including the use of sequential IF tests, as in:

{IF{MERGEFIELD Year} = 2012 {MERGEFIELD Year \* Charformat}} 

{IF{MERGEFIELD Year} = 2013 {MERGEFIELD Year \* Charformat}} 

{IF{MERGEFIELD Year} = 2014 {MERGEFIELD Year \* Charformat}}

where the M is formatted in the colour you require.

 

Another way is to use field maths & formatted numeric picture switches to control the output. In the following construction, 12 values (1-12) can be handled. The '3;1;2', '6;4;5', etc. strings show the positions of each of the 12 outputs, which you could format individually with the colour of your choice; in reality, you'd replace those digits with formatted 0s - or you could use formatted text strings, like "'March';'January';'February'", etc. (note the combination of single & double quotes).

{QUOTE{SET Val {=MOD({MergeField MyData \# 0}-1,12)+1}}{IF{=INT(Val/4)}= 0 {=Val-2 \# 3;1;2} {IF{=INT(Val/7)}= 0 {=Val-5 \# 6;4;5} {IF{=INT(Val/10)}= 0 {=Val-8 \# 9;7;8} {=Val-11 \# 12;10;11}}}}}

 

For a variation on the theme, see: http://answers.microsoft.com/en-us/office/forum/office_2010-word/conditional-formatting-a-word-document-with-merged/ffee4e5a-2caa-40f4-9f17-a7c5155f35f3

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

Sorry, but I am not understanding the first (simpler) answer.  I would like to use the sequential if statements. I can't find the syntax for coding it properly.
the sentence in my document: "congratulations, you have completed training for <<year>>."
<<year>> is placed only once on my merged document, but will have it's font color changed depending on what value it has.

Can you provide the syntax in the {{if}} stmt, for <<year>> having the value of 2012?
{IF{MERGEFIELD Year} = 2012 {black Year \* }}  

Also, is there code that must be after the \*  ?  I don't need it initcap or any of the other options that I saw in the help on the "\* " Charformat keyword.

thank you again for your patience in walking me through this code.
Karen

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

In the demo code I posted, the field brace pairs (ie '{ }') for the examples are all created in the body of the document via Ctrl-F9 (Cmd-F9 on a Mac) - you can't simply type them or copy & paste them from the message.

 

If you insert two copies of your «Year» field, thus:
«Year» «Year»
then select them and press Shift-F9, you'll see:
{MERGEFIELD Year} {MERGEFIELD Year}

Select both of those and press Ctrl-F9 and you'll see:
{ {MERGEFIELD Year} {MERGEFIELD Year} }

From there, it's simply a matter of filling in the details till you get:

{IF{MERGEFIELD Year} = 2012 {MERGEFIELD Year \* Charformat}}

Be careful to include all of the indicated spaces.

 

Hint: If you press Alt-F9, the field codes will remain displayed till you press it again. Doing that makes it easy to copy/paste the first set & paste it so all you need to change is the year # and the M formatting for the remainder.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

2 people were helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.


if I insert <<year>> <<year>> in the main document, won't my sentence then be:
"congratulations, you have completed training for 2015 2015 ."
 showing  the year twice? one in the selected color, the other in the default color that is already on the sentence? 

what is the syntax for stating in the code that the  font will now become blue in this if stmt:
IF{MERGEFIELD Year} = 2015 {MERGEFIELD Year \* Charformat}}


IF{MERGEFIELD Year} = 2015 {BLUE Year \* BLUE}}  ? The M formatting is the missing key word or variable or constant  for me. I cannot find  documentation in the  \* Charformat mention fonts. I see Initcap for example, but not colors. Where does the font (blue) go in the syntax? is it the actual word "blue" for blue font in the preceeding IF stmt? That is where I started being lost. Now I'm doubly lost by inserting the merge field twice. (pun intended).   sorry.
thanks again for your guidance.
Karen


Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

No, inserting the mergefield twice just gives you the start for creating the IF field. The first thing to do is to press Alt+F9 so that you are seeing the actual fields. So what you entered as «Year» is displayed as { MERGEFIELD Year }. Then you add text so that it looks like this:

 

IF { MERGEFIELD Year } = 2015 { MERGEFIELD Year \* Charformat }

 

Then you color the M of the second merge field as desired.

 

IF { MERGEFIELD Year } = 2015 { MERGEFIELD Year \* Charformat }

 

Then you select the entire expression and press Ctrl+F9 to convert it into an IF field, so it looks like this:

 

{ IF { MERGEFIELD Year } = 2015 { MERGEFIELD Year \* Charformat } }

Microsoft MVP (Word) since 1999
Fairhope, Alabama USA
http://ssbarnhill.com
http://wordfaqs.ssbarnhill.com
Screen shots captured with TechSmith's Snagit

1 person was helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

Thanks Suzanne.

 

Just to add to that, the construct:

IF { MERGEFIELD Year } = 2015 { MERGEFIELD Year \* Charformat }

will output:

2015

only when «Year» (or { MERGEFIELD Year }) = 2015; otherwise it won't output anything.

 

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

thank you very very much for guiding me through this. I see in your very first answer you did state the M was to be formatted as the font color, but I just didn't understand.
I have this all working now.
But in the interest of being thorough, I should add an ELSE (default)? So in several years when it goes beyond the hardcoding of years and fonts, it will still print the year.  What is the next step to convert this. It probably needs to be:
if 2014
else if 2015
else if 2016
else if 2017
else (default for all other years).
thank you again.

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

For the ELSE condition, simply add two more fields:

IF { MERGEFIELD Year } < 2015 { MERGEFIELD Year }
IF { MERGEFIELD Year } > 2017 { MERGEFIELD Year }

Again, you can use \* Charformat switches if you want these treated in a particular way.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

1 person was helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

I have a slightly different requirement.

Based on the value of a MERGEFIELD I want to put a symbol of a specific colour. So for example one of my merge fields is a relative rank. I want to show the rank and then a + symbol in GREEN for a positive movement in rank or a - symbol in RED for a negative movement in rank.

I am able to get the + or - symbol. But I am not able to colour it. I understood about colouring the value here but have not understood how I can colour some extra test that I would have added in the process.

Thanks,

Dhaval

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

It would be best if you show the construction that you are using to get the + or - symbol and then someone may suggest how it needs to be modified.
Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
Screen shots by Snagit from www.techsmith.com

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

* Please try a lower page number.

* Please enter only numbers.

* Please try a lower page number.

* Please enter only numbers.

 
 

Question Info


Last updated April 24, 2020 Views 10,710 Applies to: