none
Conditional Text in Document Automation - Word 2016 RRS feed

  • Question

  • Good Evening,

    I am trying to create a document with conditional text that appears depending on the type of security category selected: Low or Moderate. If Low security category is selected then I want certain paragraphs to appear. If Moderate security category is selected than I want certain paragraphs appear.

    Is there a way to do this in Word 2016? Do I need to create conditional fields? If so, how do I do this? What conditional statements do I need to use?

    UPDATE

    I have two templates that I'm trying to merge into one document. One template is for Low Security, the other for Moderate Security level. Each template includes a table of content and list of figure. I think there is a way to do this with conditional text, but I'm not skilled enough with "IF Statements" to understand how to do this. Consequently, I thought about creating macro buttons,see below. However, I can't see to avoid overriding the first macro button with the first one. I'm sure there's a solution, but again I'm not sure. Furthermore, I can't share a screenshot of what I have --yet because I'm not verified.  

    Thank you for your help,

    LG_GO



    • Changed type LG_GO Tuesday, July 31, 2018 8:37 PM Not Enough Replies
    • Changed type LG_GO Tuesday, July 31, 2018 8:38 PM
    • Edited by LG_GO Thursday, August 2, 2018 5:09 PM
    Monday, July 30, 2018 11:14 PM

Answers

  • I'd look at saving the two sets of text as Building Blocks. Then insert a Building Block content control where the user can select the category - and the text required would appear in the content control.

    Another possibility could be to put both sets of text into the document, each formatted with its own style (sets of styles). Clicking the button or making the choice would set the Font.Hidden property of the style(s) to true/false so that only the one set is shown.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:24 PM
    Thursday, August 2, 2018 12:10 PM
    Moderator
  • So there is a thread that verifies people's accounts. I'm adding it here just in case you come across people in similar situations. 

    https://social.msdn.microsoft.com/Forums/en-US/090972cb-b81f-498f-b718-948caca975c4/verify-account-41?forum=reportabug

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:25 PM
    Thursday, August 2, 2018 3:35 PM
  • Do note that conditionally inserting Building Blocks can have undesired results. For example, if you make an error in the selection, then change it, both Building Blocks are liable to end up in the document.

    As for toggling the hidden property of the conditional text, that, too, is unreliable; whether hidden text remains visible, or even prints if it isn't, depends on how the user has their system configured - it's not something you can control.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:26 PM
    Sunday, August 5, 2018 7:05 AM
  • If the user deletes the incorrectly-inserted one, that's not a problem; it's when they change their mind and insert the correct one without deleting the erroneous one that it becomes a problem. 

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, August 7, 2018 3:31 AM
    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:26 PM
    Monday, August 6, 2018 10:18 PM

All replies

  • This can be done using formfields, without macros, or with content controls & macros. Which are you using: formfields; or content controls?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, July 31, 2018 1:47 AM
  • Hey Paul,

    I would like to learn how to do it using both methods. Also, could you tell me if the page length would effect the success of either method? My document is approximately 300+ long, so the solution would have to be flexible and allow for me to make annual updates to the text.

    Thank you so much,

    LG_GO

    Tuesday, July 31, 2018 1:45 PM
  • For demonstrations of:

    Dependent Text using Content Controls, see:
    http://www.msofficeforums.com/word-vba/16498-multiple-entries-dropdown-lists.html#post46903
    and, for different elements from a selected item to be output to different content controls, see:
    http://www.msofficeforums.com/word-vba/16498-multiple-entries-dropdown-lists-5.html#post120392
     
    Dependent Text using Formfields, see:
    http://www.msofficeforums.com/word/16379-form-auto-fill-based-different-field-value.html#post46429

    Note, too, that Content Controls can also be used with field coding. However, Content Controls and Formfields should not be used in the same document. Doing so can lead to inconsistent behaviour. Content Controls are also only supported on Word 2007 & later (PC) & 2013 & later (Mac). Conversely, formfields & field coding are not conducive to editing the conditional text.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, July 31, 2018 9:58 PM
  • Paul,

    Thank you, I'm trying to do this using macro buttons instead. Mainly because the text will be static and I think this would require less w. Specifically, I'm trying create two macro buttons that will auto-populate a blank (macro enabled template) with Low Severity category text and Moderate Severity category text.

    Note that I've tried doing, but I think I'm doing something wrong because when I try creating the second macro for the Moderate Severity text, the previous text (macro button) is overwritten even though they're two different buttons.

    So perhaps I need to create two (2) of show/hide Low/Mod Category buttons. One macro button set would show the Low Cat text then another would hide it. Second macro button set would show the Moderate Severity Cat text then another button hides it. 

    Wednesday, August 1, 2018 9:35 PM
  • Without actually seeing your document, it's impossible to diagnose the issue or give specific advice. If care to upload it to One Drive, for example, and post a link here, I'm sure someone will look at it for you.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, August 1, 2018 10:40 PM
  • I'd look at saving the two sets of text as Building Blocks. Then insert a Building Block content control where the user can select the category - and the text required would appear in the content control.

    Another possibility could be to put both sets of text into the document, each formatted with its own style (sets of styles). Clicking the button or making the choice would set the Font.Hidden property of the style(s) to true/false so that only the one set is shown.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:24 PM
    Thursday, August 2, 2018 12:10 PM
    Moderator
  • Paul,

    Thanks, but I can't upload to OneDrive. I tried to include a screenshot of my VBA code, but I have to be verified first. I have no idea how to do this, so I'm going to be posting another question about this process. 

    Thursday, August 2, 2018 1:16 PM
  • Cindy,

    Thank you so much, I'll try this approach and tell you whether I'm successful or not. Thank you again!

    Thursday, August 2, 2018 1:17 PM
  • So there is a thread that verifies people's accounts. I'm adding it here just in case you come across people in similar situations. 

    https://social.msdn.microsoft.com/Forums/en-US/090972cb-b81f-498f-b718-948caca975c4/verify-account-41?forum=reportabug

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:25 PM
    Thursday, August 2, 2018 3:35 PM
  • Hey Cindy,

    So, I've tried the following approach and it did work--for the most part. However, my table of contents are messed up and I'm not sure how to fix them. That said, I think there is a way to do it, so I'll keep working on it. 

    <I'd look at saving the two sets of text as Building Blocks. Then insert a Building Block content control where the user can select the category - and the text required would appear in the content control.>

    In the interim, could you tell me or direct me to resources for executing the following approach? 

    <Another possibility could be to put both sets of text into the document, each formatted with its own style (sets of styles). Clicking the button or making the choice would set the Font.Hidden property of the style(s) to true/false so that only the one set is shown.>

    Thank you again for your assistance,

    LG_GO

    Thursday, August 2, 2018 4:27 PM
  • How the TOC "is messed up"?

    Difficult to give you any direction since no one has any idea how the conditional part(s) of the document need to be set up, what formatting is involved, or how the TOC (a new factor you didn't mention before) relies on this stuff. Are you familiar with Styles in Word? That's an end-user functionality and is explained in the Help and various tutorials.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Thursday, August 2, 2018 4:39 PM
    Moderator
  • Cindy,

    Oh ok, you're talking about font styles. Yes, I'm definitely familiar with those. However, in this case I'm talking about conditional text appearing depending on the Low/Moderate categories. The Building Blocks would work if it retained the format, but I don't think it can in this case. The macro solution would work if I could figure out how to hide/unhide text. I am trying to get my account verified to actually provide a screenshot.

    Thursday, August 2, 2018 5:02 PM
  • How are building blocks not retaining the format? In my experience they do/should...

    As to macros and hiding/unhiding text: as I said, set up two sets of identical styles, just with different names. Base each on a specific style, so that if you set that style's Font.Hidden = true it will "cascade" through the set. Based on the category, the code changes that setting for both style sets: one set disappears, the other is visible.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Saturday, August 4, 2018 11:41 AM
    Moderator
  • Do note that conditionally inserting Building Blocks can have undesired results. For example, if you make an error in the selection, then change it, both Building Blocks are liable to end up in the document.

    As for toggling the hidden property of the conditional text, that, too, is unreliable; whether hidden text remains visible, or even prints if it isn't, depends on how the user has their system configured - it's not something you can control.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:26 PM
    Sunday, August 5, 2018 7:05 AM
  • And do note that form field codes can also be displayed in the document or when pritning depending on how the user has their system configured.

    But this is something that CAN be controlled (as well as anything can) by running a macro when printing / saving / opening, etc. a document.

    And if Building Block Content Controls are used, (as I suggested, not just building blocks) then the problem described is not a factor.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Sunday, August 5, 2018 9:36 AM
    Moderator
  • Cindy,

    <How are building blocks not retaining the format? In my experience they do/should...>

    Yes, I was able to create two separate building blocks for Low and Moderate Security categories. However, I experienced the following problems. First, I saved it as a Normal template then I selected the option to Update entire table. Consequently, all my TOC fields and text fields got toggled. I was able to toggle them back, but I had to delete the Normal template to restore the MS Office Normal Template. 

    

    The second time, I selected Update page numbers only after creating both building blocks then I saved it as Building Blocks not Normal. I would like to save these building blocks in a Template, but I can't risk it affecting all my other "Normal" documents. Is there a way to do that? 

    Monday, August 6, 2018 3:47 PM
  • Cindy,

    <As to macros and hiding/unhiding text: as I said, set up two sets of identical styles, just with different names. Base each on a specific style, so that if you set that style's Font.Hidden = true it will "cascade" through the set. Based on the category, the code changes that setting for both style sets: one set disappears, the other is visible.>

    I'm not sure I understand enough about macros to understand how to write the script on Macro's back-end on to execute this solution. Can you provide a screenshot or write out the script? 

    Monday, August 6, 2018 4:05 PM
  • Paul -<Do note that conditionally inserting Building Blocks can have undesired results. For example, if you make an error in the selection, then change it, both Building Blocks are liable to end up in the document.>

    Cindy -<But this is something that CAN be controlled (as well as anything can) by running a macro when printing / saving / opening, etc. a document.

    And if Building Block Content Controls are used, (as I suggested, not just building blocks) then the problem described is not a factor.>

    Question

    Why do I need to use Building Block Content Controls vs just Building Blocks? If a user selects the wrong building block then deletes it, why would they still see the wrong building block when they print? 

    Thank you both,

    LG_GO

    Monday, August 6, 2018 4:08 PM
  • If the user deletes the incorrectly-inserted one, that's not a problem; it's when they change their mind and insert the correct one without deleting the erroneous one that it becomes a problem. 

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, August 7, 2018 3:31 AM
    • Marked as answer by LG_GO Wednesday, August 8, 2018 2:26 PM
    Monday, August 6, 2018 10:18 PM
  • Paul, 

    Thanks for confirming. 

    Monday, August 6, 2018 10:20 PM
  • Cindy,

    Here's a screenshot for the macro button I created for pasting Low Security Category text. When I create a macro button for pasting in Low Category text it works fine, but when I try creating a macro for pasting in the Moderate Security text my previous macro gets overridden. I'm trying to make sure they both operate separately from each other, but I'm not sure how to write a script to do this. I don't know enough about macros to create two different buttons to run two separate functions in one template or document.  

     

    Monday, August 6, 2018 10:27 PM