Search code examples
vbams-wordmailmergemergefield

Conditional formatting on mergefield - it just prints the code to the output instead rendering


I inserted a merge field into my MS Word file like below:

enter image description here

In the output, it is seen like below:

enter image description here

So it doesn't render the code (if statement) but prints it out to the output. Why and how can I fix it? Thanks.

Edit: note that the curly paranthesis are created by ctrl + f9


Solution

  • That suggests you have Word's field code display toggled on and that you're only previewing the merge, rather than completing it. Word's field code display can be toggled on/off via Alt-F9.

    Your display also indicates that the scode field outputs spaces after the number. You need to either eliminate those spaces from the data source or allow for them in the test. You can allow for them in one of two ways:

    {IF{MERGEFIELD scode \# 0}= 301 "jesus" jesus2"}
    

    or:

    {IF "{MERGEFIELD scode}" = "301*" "jesus" jesus2"}