Share via

Conditional Text using IF fields and Content Controls

Anonymous
2011-12-20T04:00:04+00:00

I am trying to create a document that will display three different text messages when a numeric value, either selected from a drop-down list content control or typed into a rich text content control, is either below a certain value, above a certain value, or between two values. For example, a question will prompt the person to enter the amount of hours they slept the night previously, and the desired effect is that a risk level alert should appear, as follows:

< 5 Hours - High Risk

5-6 Hours - Moderate Risk

=> 7 Hours - Low Risk

  • not dissimilar to password security alerts when the strength of the new password is weak, moderate or strong...

I have tried using Ref fields nested in IF fields, to no avail. Several threads detail problems similar to this, but few solutions have been identified - and for those that are identified, the solution still does not work. This is also the first time I've attempted using some of the more intricate Word functions (I'm hopeless with code, too).

Your help would be greatly appreciated, thanks!

C

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-12-30T03:08:48+00:00

You just make a case for each control:

Private Sub Document_ContentControlBeforeStoreUpdate(ByVal CC As ContentControl, Content As String)

Dim strRisk As String

Select Case CC.Title

Case "Sleep Last Night"

  Select Case CLng(CC.Range.Text)

Case Is < 5

strRisk = "High Risk"

Case Is = 5, 6

strRisk = "Moderate Risk"

Case Else

strRisk = "Low Risk"

End Select

ActiveDocument.CustomXMLParts(4).SelectSingleNode("ns0:ccMap[1]/ns0:CCMapChild_" & CC.Title & "RiskA[1]").Text = strRisk

Case "Sleep 48 Hours Ago"

  Select Case CLng(CC.Range.Text)

Case Is < 6

strRisk = "High Risk"

Case Is = 7,8

strRisk = "Moderate Risk"

Case Else

strRisk = "Low Risk"

End Select

ActiveDocument.CustomXMLParts(4).SelectSingleNode("ns0:ccMap[1]/ns0:CCMapChild_" & CC.Title & "RiskB[1]").Text = strRisk

End Select

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

20 additional answers

Sort by: Most helpful
  1. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2011-12-21T04:47:37+00:00

    Hi C Rohde,

    With a dropdown formfield whose:

    • internal bookmark name is Dropdown1;

    • properties are set to 'calculate on exit'; and

    • input values are simple digits (eg 0, through 9),

    you could use a formula field coded as:

    {=INT(({REF Dropdown1}-3)/2)-1 # "'High Risk';'Low Risk';'Moderate Risk'"}

    Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-12-20T12:53:31+00:00

    Yes that can be done but it involves mapping (or binding) the content controls to a customXML part and using one of the ContentControl built-in events.

    Using my http://gregmaxey.mvps.org/ContentControl_Variable_Bookmark_and_Document_Property_Tools_Add_In.htm, create a mapped dropdown CC (titled "Sleep") containing the values 1 through 12 and a mapped plain text control (titled "Risk"). 

    In the "ThisDocument" module of the VBA project use:

    Private Sub Document_ContentControlBeforeStoreUpdate(ByVal CC As ContentControl, Content As String)

    Dim strRisk As String

    Select Case CC.Title

      Case "Sleep"

        Select Case CLng(CC.Range.Text)

          Case Is < 5

            strRisk = "High Risk"

          Case Is = 5, 6

            strRisk = "Moderate Risk"

          Case Else

            strRisk = "Low Risk"

        End Select

    End Select

    ActiveDocument.CustomXMLParts(4).SelectSingleNode("ns0:ccMap[1]/ns0:CCMapChild_Risk[1]").Text = strRisk

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-12-20T06:22:37+00:00

    Thanks for your timely reply, Charles.

    Is there any way for the fields to automatically update as their referenced data is being updated (e.g. If someone selects 3 hours, is there any way the field would automatically update to say 'High Risk'?)

    Thanks again.

    C

    Was this answer helpful?

    0 comments No comments
  4. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2011-12-20T05:37:54+00:00

    With IF fields it often helps if you put your Ref field bookmark name in quotation marks. See Calculated Dates in Word for a sample field using REF fields in IF fields.

    You may also want to look ta Fields in Microsoft Word. I know that the official Microsoft field reference does not call for quotation marks, but it gives more reliable results.

    You are right that working with complex fields feels a lot like programming. Without seeing your fields it is hard to help you debug them.

    Was this answer helpful?

    0 comments No comments