Tip #716: Conditional values in Word templates

Flying pig thumbnailWord templates introduced in CRM Online and CRM 2016 are not the easiest contraptions to edit. One of the questions that popped up in a recent conversation was how to use conditional IF in the document generation.

tl;dr

To refer to the control values after the document has been generated, wrap content controls using bookmarks defined at the control container level.

Longer

CRM fields are defined as Content Controls and in theory we could have written some simple VBA using ContentControl object. Except that CRM does not play nice when generating the document and simply replaces all content controls with the record values.

The solution is to wrap the content controls using the bookmarks defined at the container level, i.e. cell, row, paragraph, etc. Then this bookmark can be used as a reference in a conditional expression of the IF field.

For example, to insert conditional text depending on the total invoice value:

  1. Extract the Invoice template that comes with the sample data and save it under a different name.
  2. Open the template, find total amount, select the entire table cell and click Insert > Bookmark.
  3. Type totalbookmark as a bookmark name, click Add.
    Insert bookmark
  4. Delete the words “Thank you for your business” and click Insert > Quick Part > Field.
  5. Select IF field and type the following field code:
    IF totalbookmark > 100 “Thank you for you business!” “Thanks for nothing!”
  6. Click OK. Hint: to see and edit field codes in the document, press Alt-F9.
  7. Save the file and upload to CRM as a new template.
  8. Create an invoice and generate the invoice document.
  9. Enable document for editing, press Ctrl-A to select the entire content and press F9 to refresh the field content
  10. Depending on the total value of the invoice, the document will say “Thank you for your business!” or “Thanks for nothing!”
    Conditional values in the template

It’s not a full automation that potentially could be improved with some VBA (I’m not 100% sure if it will work) but it’s a step in the right direction and a cheap way to add some pizazz to your documents.

16 thoughts on “Tip #716: Conditional values in Word templates

  1. Edwin says:

    Has anyone gotten this to work without having to execute step 9

    “Enable document for editing, press Ctrl-A to select the entire content and press F9 to refresh the field content”

    This is a great tip, but the functionality would be complete if conditional text can be presented in a document if you did not have to refresh the field content.

    Thanks!

    • Edwin,

      that would indeed complete the functionality, however, automatic field updating requires macros. Latest versions of Word do not support saving macros in docx files, only docm and you will not be able to upload this type of files to CRM. And, in general, macros are evil 🙂

      Cheers
      George

  2. I guess the real tip of the day here would be to look for a good third-party add-on if you want to insert conditional content in a Word template. XperiDo probably has the most user-friendly template design add-in for Word to set conditions.

  3. Harsha Pawar says:

    Can we hide and show table rows in word as per field values present in table?

    • If you wrap the table in IF field but I don’t know how to deal with individual rows. If lots of table manipulation is required, I’d consider Excel. Or even 3rd party products.

      • Harsha Pawar says:

        I tried lot of things, VBA coding and if you put VBA coding it change file extension. which is not supported by dynamics crm. Talk with Microsoft team and they said this is their product limitation. I hope inn future they will provide filtering inactive records or sorting in word templates.

  4. Ronan Raftery says:

    Hi all,

    Tried recreating this in a CRM trial but having a blocker. The Bookmark keeps getting deleted when the document is generated. Is this something you came up against when testing yourself?

    I tried applying the bookmarks to cells and paragraphs as mentioned, but the bookmark is deleted every time if there is a content control inside.

    Is there anything I’m missing here? It’s an online trial so obviously it’s the latest version of CRM, and I’m using Word 2016.

    Cheers,
    Ronan

    • Ronan,

      bookmarks will be deleted unless they are “defined at the control container level”. For example, “select the entire table cell” is quite important step. Just experiment a bit, you’ll find the right container where bookmarks are preserved.

      Cheers
      George

      • Danieel says:

        Hi George,

        i’ve tried 200 times but the Ronan’s problem still remains to me : there are no bookmarks in the download invoice and the conditional field doesn’t work.

        I have selected the entire table cell; I’ve clicked Ctrl + A then F9 but it doesn’t do anything.

        If I click on “refresh field” on the conditional field it becomes false either if it isn’t.

        My conditional expression is “totalamount > 4000 “is grater then 4000” “is not grater then 4000”.

        Thanks for the patience,
        hope you’ll solve our problem

        Daniele

        • Daniele,

          it worked quite a few times for me. The only reason why bookmark gets deleted is that it is not defined at the control container level.

          Just try experimenting with the selection when defining a bookmark. I found that, when a cell is selected in the template before you define the bookmark, an extra Shift-RightArrow might just do the trick (meaning that you selected the content of the cell, nudging selection one extra character will select the cell which is what’s required).

          HTH
          George

  5. schi says:

    Thanks for the tip, it works nicely for me.

    However, unfortunately, it seems as if the bookmark / content control object need to be visible.

    What I would like to do is to create some salutation based on the CRM gender information without displaying the gender information text itself. But as soon as I use the “hidden” text effect for the reference content control object / bookmark, the information seems to be no longer available to the if condition, so it results to be always false.

    • I wouldn’t do it if Word even if it worked. Salutation is a very personal and finicky thing to rely on the template to get it correctly. IF you insist on automatic rules, I would add calculated Salutation field to CRM and extract it from there.

  6. Jaithun Sheik says:

    How to remove the space if there is no data in the dynamic field in the crm in word document template?

  7. Would like to use check box for yes/no option set, but can’t seem to get it to work? Any suggestions?

  8. Jindrich says:

    May I ask you if there is any way how to insert into the true or false value a bookmark? For instance “Thanks for nothing! totalbookmark” but this does not work for me.

    I’m referring to step no. 5

    IF totalbookmark > 100 “Thank you for your business!” “Thanks for nothing!”

    In my case, I’d need to place into the false string a field value from Dynamics but it does not work so I’m trying to represent that value as a bookmark within the Word Template but with the same result. Dynamics always displays only bookmark or field name.

    “Payable within payabledaysbookmark days of receipt”

Leave a Reply

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