Repeating section content control in Word 2013

Some significant improvements were made to Word’s content controls in Word 2013. You can find an overview here.

One interesting capability is a new kind of content control that brings back some of the functionality lost when XML nodes technology had to be pulled from Word due to the court decision in 2009. In the original release of Word 2007, xml nodes inserted into a table could be configured to repeat automatically when new table rows were inserted. While content controls in Word 2007 and 2010 can take over a lot of what could be done with xml nodes, this – and validation – were not possible “out of the box”.

A large portion of the article linked to above covers the new type of content control, in the section “Supporting repeating content”. You’ll also find a practical introduction, mainly targeting the “power user” on Greg Maxey’s site.

Something that doesn’t really jump out at you in any of the information I’ve found so far is how to

  1. Map a repeating section to a list of data already present in a Custom XML Part so that it all displays in the document
  2. Trap when the user adds or deletes a new data item (the repeating section must be mapped to a Custom XML Part in order for this to work)

I’ll cover the first point in this post and the second in a follow-up blog post.

Map a repeating section to existing data

The sample Zip file contains a Word document that has a table with content controls, a custom XML part to which the content controls should be mapped and VBA code for doing the mapping. The source for the custom XML Part is the BookList.xml sample file.
The table displays the book list information: title, list of authors, publisher and ISBN number. The document in the screen shot below is in Design mode so that the content control “nodes” are visible. The second column, for the list of authors, contains two content controls. “Author” is a plain text content control; “Authors” is a repeating section content control. This will allow picking up multiple authors from the XML. The repeating section content control for the entire list of books will be added by the code.
Notice the order in which things are done:

  • First, the controls in the table cells are mapped to the first set of repeating items in the custom XML part.
  • Next, the repeating section control is assigned to the table row.
  • Only then is the repeating section control mapped to the XPath for the items that will be repeated.

The repeated section content control can only be mapped after any content controls it contains have been mapped. The final result:

If the user clicks on the “+” at the right of a table row, a new row with empty content controls will be generated. At the same time, a new “book” node will be created in the Custom XML Part. As information is entered into the individual content controls it will be written to the Custom XML Part.

The code:
Sub DemoRepSecCC()
Dim doc As Word.Document
Dim sPathXML As String
Dim cxp As Office.CustomXMLPart

Set doc = ActiveDocument
sPathXML = doc.Path & "\BookList.xml"
Set cxp = doc.CustomXMLParts(4) ' CreateCustomXMLPart(doc, sPathXML)
Debug.Print MapContentControls(doc, cxp)
End Sub

Private Function CreateCustomXMLPart(doc As Word.Document, sPathXML As String) _
As Office.CustomXMLPart
Dim cxp As Office.CustomXMLPart

Set cxp = doc.CustomXMLParts.Add
cxp.Load sPathXML
Set CreateCustomXMLPart = cxp
End Function

Private Function MapContentControls(doc As Word.Document, _
cxp As Office.CustomXMLPart) As Boolean
Dim rng As Word.Range
Dim ccRepSec As Word.ContentControl

Set rng = doc.Tables(1).Rows(2).Range
doc.SelectContentControlsByTitle("Title").Item(1).XMLMapping.SetMapping _
doc.SelectContentControlsByTitle("Publisher").Item(1).XMLMapping.SetMapping _
doc.SelectContentControlsByTitle("ISBN").Item(1).XMLMapping.SetMapping _
doc.SelectContentControlsByTitle("Author").Item(1).XMLMapping.SetMapping _
doc.SelectContentControlsByTitle("Authors").Item(1).XMLMapping.SetMapping _
Set ccRepSec = rng.ContentControls.Add( _
Word.WdContentControlType.wdContentControlRepeatingSection, rng)
ccRepSec.XMLMapping.SetMapping "//books/book"
End Function

2 Responses to “Repeating section content control in Word 2013”

  1.   Victor Says:

    Hi ,
    my name victor .i have a problem to use word template , here i describe my scenario

    My First scenario

    i have create a word template with multiple tables using styles .
    i create xml file for nodes Ex:
    i call this xml file using xml mapping and i load my custom xml files and mapping nodes in to table cells.
    if mapping xml nodes it shows only place holder text ” Click here to enter text ” . But , inned to show End and start tags .

    My Second Scenario

    Using i’m able to find base name like ” XMlnode.XMLMapping.CustomXMLNode.BaseName ”
    then replace my text . After that i insert new row wordDoc.ActiveWindow.Selection.InsertRowsBelow()

    then i repeat table to find base names that time it doesn’t find new row base names ( it doesn’t carry the mapping values to new row )

    can you please guide how can i insert new row with mapping nodes ..

    •   WordMeister Says:

      Hi Victor
      I apologize for the late reply. I thought WordPress would notify me when comments come in, but that hasn’t been the case…
      I recommend you ask this question on the Word for Deveopers forum () where you can get a good discussion going. When you ask, be sure to mention the VERSION of Word you’re targeting as this is very important for your questions.
      However, I can tell you that – if I understand you correctly – both of these things are built into Word 2013.

Leave a Reply