How Do I Populate a List from a Repeating Section Content Control Form Field in Word 2016?

So I have a repeating section content control (RSCC) with two text content controls in it. One is a plain text content control for the name of an item, and one is a rich text content control for a description of the item.

This is in an RSCC because in some cases, multiple items will need to be written about. I'd like to be able to reference each of these potential item names later on in the document. How do I do this?

Ideally, the user punches in a name (like "widget 1"), and then later in the document, there's a paragraph that says something like "the item(s)–widget 1– blah blah blah...". However, if the User punches in "widget 1" for the item name, then utilizes the RSCC to name and describe a "widget 2", the above paragraph now says "the item(s)–widget 1, widget 2—blah blah blah..." and so on. Is this possible? If so, how do I do it?

1 Answer

I am way out of my depth on this — I’ve never even heard of RSCCs before I saw your question — but I’ve cobbled together something that may be part of your solution.  I assume that your data are in a table, and that your “name” field is column 1.  Enter this VBA code:

Sub listControl_1()
    Dim o As Table
    Dim c As Row
    Dim mylist As Variant

    Set o = ActiveDocument.ContentControls.Item(1).Range.Tables(1)
    rownum = 0
    For Each c In o.Rows
        rownum = rownum + 1
        If rownum > 1 Then
            thislen = Len(c.Cells(1).Range.Text)
            If thislen > 0 Then
                thislen = thislen - 1
            End If
            If rownum = 2 Then
                mylist = Mid(c.Cells(1).Range.Text, 1, thislen)
            Else
                mylist = mylist & ", " & Mid(c.Cells(1).Range.Text, 1, thislen)
            End If
        End If
    Next c
    MsgBox mylist
End Sub

The If rownum > 1 Then test assumes that your table has a header row (and skips it, so you don’t get the header text in the list).  If you don’t have a header row (or if you have more than one), adjust the code accordingly.  Each cell seems to have a delimiter character at the end; the code uses Mid() to strip that off.

This assembles a comma-separated list of the values for column 1 of the first table, and displays it in a message box.  If your data are not in a table, I don’t know what to tell you.  If your data are in a table, but it’s not the first one in the document, I don’t know how to find the right one.  And, most importantly, I don’t know how to get the list into your document.  As I said, this is at best a part of your solution.

I copied the framework of the VBA code from MSDN’s “How to get a value from content control within Repeating section content control”.  (There’s more code there, which you might find useful.)  Note that their code does Mid(text, 1, Len(text) - 2).  I tried subtracting 2 from the length, and I lost the last character of the text; I don’t know why it’s different.  Also I notice that their code does c.Cells(columnNumber).Range.Text = someText to write data into the table.  Perhaps you can use this as a clue to figure out how to get the result from the routine into your document.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest