Share via

Conditional Text display with Plain text Content Control

Anonymous
2017-05-15T06:34:15+00:00

I am working a word template which using plain text content control and XML mapping. Here, I want to use the third plain text content contral to display the first plain text content if the second plain text content is empty; otherwise display the second plain text content. Or should I use IF field instead of the third plain text content control? What is coding? 

Thank you very much!

***Post moved by the moderator to the appropriate forum category.***

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

7 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2017-05-22T08:33:05+00:00

    Did you use CTRL+F9 to create the field delimiters around cc1 and cc2?

    You cannot use the { and } and you enter via the keyboard.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-05-22T06:40:31+00:00

    Thank you. I tried, but it only return "{cc1}" or "{cc2}". I don't know where I made mistake...

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-05-15T19:42:40+00:00

    It is possible to point the content control to different nodes in the XML depending on what is in other nodes, but the problem is that Word does not update the results how you might hope, because it only updates the values of content controls already connected to the node whose value has changed. It does not update the values of content controls whose XPath will now change to point to that node.

    It's probably easier to see with some sample ccs and xml, e.g. in a new blank document run experiment1_part1, put some values in the content controls, and you will see that cc4 does not update how you hope. However, at any point, if you run experiment1_part2, cc4 should now point to the correct node and its value should be what you expect. So you would need to consider using either contentcontrol events orxml store events to trigger updates to the controls.

    In some respects it would nice if CCs could "autoupdate", but I imagine the Word developers realised that in the general case it would either mean updating every content control, every time something in the XML store changed, or trying to implement the kind of strategy a spreadsheet might implement (probably maintain a graph of cell dependencies, try to optimise recalculation, and have a mechanism to deal with loops in the graph). 

    Sub experiment1_part1()

    Dim cc As Word.ContentControl

    Dim cxp As Office.CustomXMLPart

    Dim i As Integer

    Dim rng As Word.Range

    Dim xml As String

    With ActiveDocument

      ' clear out previous experiments

      ' remove ccs

      For i = .ContentControls.Count To 1 Step -1

        With .ContentControls(i)

          .LockContentControl = False

          .LockContents = False

          .Delete

        End With

      Next

      ' remove content

      .Content.Text = ""

      ' remove non-builtin CXPs

      For i = .CustomXMLParts.Count To 1 Step -1

        If Not .CustomXMLParts(i).BuiltIn Then

          .CustomXMLParts(i).Delete

        End If

      Next

      xml = "<?xml version='1.0' encoding='UTF-8'?>" & vbCrLf

      xml = xml & "<root>" & vbCrLf

      xml = xml & "<cc></cc>" & vbCrLf

      xml = xml & "<cc></cc>" & vbCrLf

      xml = xml & "<cc></cc>" & vbCrLf

      xml = xml & "</root>" & vbCrLf

      Set cxp = ActiveDocument.CustomXMLParts.Add(xml)

      Set cc = .ContentControls.Add(wdContentControlText)

      With cc

        .XMLMapping.SetMapping "/root[1]/cc[1]", , cxp

        .Title = "cc1"

        .LockContentControl = True

      End With

      Set cc = Nothing

      .Content.InsertParagraphAfter

      Set rng = .Content

      rng.Collapse WdCollapseDirection.wdCollapseEnd

      Set cc = .ContentControls.Add(wdContentControlText, rng)

      With cc

        .XMLMapping.SetMapping "/root[1]/cc[2]", , cxp

        .Title = "cc2"

        .LockContentControl = True

      End With

      Set cc = Nothing

      .Content.InsertParagraphAfter

      Set rng = .Content

      rng.Collapse WdCollapseDirection.wdCollapseEnd

      Set cc = .ContentControls.Add(wdContentControlText, rng)

      With cc

        .XMLMapping.SetMapping "/root[1]/cc[1]", , cxp

        .Title = "=cc1"

        .LockContentControl = True

        .LockContents = True

      End With

      Set cc = Nothing

      .Content.InsertParagraphAfter

      Set rng = .Content

      rng.Collapse WdCollapseDirection.wdCollapseEnd

      Set cc = .ContentControls.Add(wdContentControlText, rng)

      With cc

        ' one way to do the mapping...

        .XMLMapping.SetMapping "/root[1]/cc[1+boolean(string(/root[1]/cc[2]))]", , cxp

        .Title = "cc4:cc2, or cc1 if cc2 is empty"

        .LockContentControl = True

        .LockContents = True

      End With

      Set cc = Nothing

      .Content.InsertParagraphAfter

      Set rng = Nothing

      Set cxp = Nothing

    End With

    End Sub

    Sub experiment1_part2()

    Dim cc As ContentControl

    With ActiveDocument

      For Each cc In .ContentControls

        cc.XMLMapping.SetMapping cc.XMLMapping.XPath

      Next

    End With

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2017-05-15T16:05:55+00:00

    I don't think you can apply any logic like that to XML mapping of content controls. Your suggestion to use an IF field instead of the third content control is workable. You'll need bookmarks around the first two controls so you'll have something to refer to. Then you can set up the field like this:

    A content control is never "empty"; if there is nothing entered, it shows the placeholder text. To create the string for the comparison in the IF field, copy and paste the content of the second bookmark. If you've gone into Design Mode and changed the placeholder text, then the copy in the IF field should be the modified text.

    The drawback to this setup is that, unlike content controls that are mapped to the same XML node, the field won't update automatically. You can work around that by writing a Document_ContentControlOnExit event handler that updates the field, but then the template must be in a macro-enabled format (*.dotm) and users must be set up to enable the macro.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2017-05-15T13:09:54+00:00

    Hi Erin,

    We understand your specific need with plain text control. For detailed information about content controls please check the following articles:  About content controls, Create forms that users complete or print in Word and Content controls in Word.

    To answer your questions on mapping content controls to XML in a document file, please click here and find the "Answer" posted by Jay Freedman.

    Fell free to post back should you have other concerns.

    Was this answer helpful?

    0 comments No comments