2

Using MS Word 2010 I am trying to place an INCLUDEPICTURE field into a block of an IF statement. While both the IF statement and the INCLUDEPICTURE work correctly separate, they do not work in combination.

IF Statement:

{ IF { MERGEFIELD condition \* MERGEFORMAT } = "expression" "true" "false" \* MERGEFORMAT }

This works correctly.

INCLUDEPICTURE:

{ INCLUDEPICTURE "picture.png" }

This works correctly, too.

Combination of the two:

{ IF { MERGEFIELD condition \* MERGEFORMAT } = "expression" "{ INCLUDEPICTURE "picture.png" }" "false" \* MERGEFORMAT }

This does not work. If the IF expression is true, nothing is displayed at all.

How can I combine both the IF statement and the INCLUDEPICTURE command?

user3065343
  • 21
  • 1
  • 1
  • 2

3 Answers3

4

This is a well known-problem (i.e. you are right, it doesn't work).

Unfortunately, there isn't a particularly good solution - the simplest involves using a blank 1-pixel image file.

The usual starting point is to invert the nesting so that you have something more like this...

{ INCLUDEPICTURE "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" }" }" \d }

This always tries to insert a picture, and will report (and insert) an error in the case where { MERGEFIELD condition } <> "expression". The simplest resolution is to have a blank 1-pixel picture that you can include instead, e.g.

{ INCLUDEPICTURE "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }" \d }

It is sometimes clearer to remove the test and assignment and do it separately, particularly if there are multiple tests. In this case,

{ SET picname "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }

or if you prefer,

{ IF "{ MERGEFIELD condition }" = "expression" "{ SET picname "picture.png" }" "{ SET picname "blank1.png" }" }

You still need an IF nested inside the INNCLUDEPICTURE to make it work. You can use:

{ INCLUDEPICTURE "{ IF TRUE { picname } }" \d }

If you merge those nested fields to an output document, the fields will remain in the output. If you want the fields to be resolved (e.g. because you need to send the output to someone who does not have the image files) then you need something more like this:

{ IF { INCLUDEPICTURE "{ IF TRUE { picname } }" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }

I believe you can reduce this to

{ IF { INCLUDEPICTURE "{ picname }" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }

In fact, I believe you can insert the full path+name of any graphic file that you know exists instead of the first { picname }, e.g.

{ IF { INCLUDEPICTURE "the full pathname of blank1.png" } { INCLUDEPICTURE "{ IF TRUE { picname } }" \d } }

But you should check that those work for you.

EDIT FWIW, some recent tests suggest that whereas the pictures appear unlinked, a save/re-open displays a reconstituted link (with a *MERGEFORMATINET near the end), and the pictures are expected to be at the locaitons indicated in those links. Whether this is due to a change in Word I cannot tell. If anything has changed, it looks to be an attempt to allow some relative path addressing in the Relationship records that Word creates inside the .docx.

Some observations...

  • Make sure paths have doubled-up backslashes, e.g. c:\\mypath\\blank1.png . This is usually necessary for any paths hard-coded into fields. For paths that come in via nested field codes, please check.
  • As a general point, it is easier to work with INCLUDEPICTURE fields when the document is a .doc, not .docx, and to ensure that File->Options->Advanced->General->Web options->Files->"Update links on save" is checked. Otherwise, Word is more likely to replace INCLUDEPICTURE fields with a result that cannot be redisplayed as a field using Alt-F9
  • When you want to treat the comparands in an IF field as strings, it is advisable to surround them with double-quotes, as I have done. Otherwise, a { MERGEFIELD } field that resolves to the name of a bookmark may not behave as you would hope. Otherwise, spacing and quoting is largely a matter of personal choice.

So far, none of these field constructions will deal with the situation where you have path names for pictures that may or may not exist. If that is what you need, please modify your original question.

  • i am using your methodology above and it works when the condition is true but the image never refreshes when i do the F9. Do you know why? The code im using is `{ INCLUDEPICTURE "{ IF "{ MERGEFIELD X}" = "True" "{ MERGEFIELD blank }" " { MERGEFIELD path }" }" \d}` – JT2013 Aug 14 '14 at 16:39
  • The problem is often "spaces in the file path+name that should not be there". FOr example, if you are actually using the coding you have quoted in your comment, there is a space before the { MERGEFIELD path } field that should not be there. (Of course, that may well be a typo, but even so, that is what I would look for first). The other main possibility is that the value of X is not "True" but "TRUE", "true", "-1" or whatever. –  Aug 14 '14 at 18:35
  • In word 2013, you can Unlink the image by removing the \d .. i.e. { INCLUDEPICTURE "{ IF TRUE { picname } }" } ... Also this is the only way I could unlink, I could not get your suggestion to work. – eddyparkinson Jul 16 '15 at 08:26
  • @eddyparkinson: did your own research+Answer resolve your comment? If so, fine. If not, my first observation would be that some of this stuff only works with .doc, not .docx, and even that may change following so-called "fixes". –  Jul 16 '15 at 18:41
  • bibadia, you are right, "word version/doc/docx" cause a complication I used docx & word 2013. Would be good to know what others have used. – eddyparkinson Jul 16 '15 at 23:47
  • @eddyparkinson: it is actually a while since I visited this, and now I do, I find that the "longest" set of fields *appears* to work OK when I first merge - i.e. no field codes remain in the document, but after save+re-open the links are reconstituted. The images only appear on re-open if they are where those links expect them to be. I have a feeling something in this area has changed (again) but am not sure. For the most part, what actually now works is to use the long set of fields and omit the \d, but in that case, images with UNC pathnames (and perhaps others) are not picked up. –  Mar 05 '16 at 13:57
0

Step by step guide:
bibadia's answer works, but word does not tell you when you make mistakes, so it is very hard to get it right. So I hope this step by step answer helps.

Step 1: Add a Picture
In Word 2013 docx (no idea about other versions) add

{ INCLUDEPICTURE "C:\\picture.png" }  

Note: Use CTRL+F9 to add { } , don't ever type them in, as they will not work. Use \\ and not \ Run the mail merge, do Ctrl+A then F9 to show the picture.

Step 2: Auto Show it
To change the mail merge document use (CTRL+A Shift+F9). Change it to

{ SET picname "C:\\picture.png" }
{ INCLUDEPICTURE "{ IF TRUE { picname } }" \d }

Run the mail merge - the picture should show up, no need for Ctrl+A then F9

Step 3: Unlink it
Remove the \d

This will let you email the doc. As the \d causes the document to create a link to the image file, rather than include it.

Step 4: add an IF Use bibadia's solution, i.e.

{ SET picname "{ IF "{ MERGEFIELD condition }" = "expression" "picture.png" "blank1.png" }" }
eddyparkinson
  • 3,680
  • 4
  • 26
  • 52
0

Another option that I've tested works is to use an If statement to check an expression (In my example check if the entry is not null), and if not then display the image, if not display some custom text (If you don't want text just have empty quotation marks i.e. ""):

{IF {MERGEFIELD my_photo_variable_name} <> "" {INCLUDEPICTURE "{IF TRUE {MERGEFIELD my_photo_variable_name}}" \d} "Text to display if no picture available"}

Which translates as:

If there is no value for the image my_photo_variable_name, include the image in the mail merge.

If there is no value i.e no image, then display custom text Text to display if no picture available.

algorhythm
  • 3,304
  • 6
  • 36
  • 56