Changing the content of multiple ContentControls at once

144 views Asked by At

I have a document with a ContentControl containing a dropdown list of six items, that is duplicated many times through one document.

I want to replace the list items.

Each value of the list stays at the same position and should stay selected as before; they should just get a new name (e.g. changing values A-F into 1-6; everywhere I selected D, should now be selected 4)

I found this but it's not executable. I pasted this into a new module.

It doesn't have to be a VBA approach, I'd be fine with a find&replace method or other built-in tool.
It can be a VBA approach, if there is not much understanding/editing needed on my side.

This is my list:
enter image description here

I am not able to set a value that differs from the name, they are always the same. When I type in one, the other is typed simultaneously.

1

There are 1 answers

5
Tim Williams On

EDIT - fixed the issue with re-setting the value after mapping the list to the new values.

Here's something for you to work with:

Sub Tester()
    Dim cc As ContentControl
    
    For Each cc In ActiveDocument.ContentControls
        If cc.Type = wdContentControlDropdownList Then
            UpdateListItems cc
        End If
    Next
End Sub

Sub UpdateListItems(cc As ContentControl)
    Dim le As ContentControlListEntry, currVal, newVal, rngVal, txt
    Dim sel As ContentControlListEntry
    currVal = cc.Range.Text
    Debug.Print "---------------------------------"
    Debug.Print "Control", cc.Title, "Current='" & currVal & "'"
    For Each le In cc.DropdownListEntries
        txt = le.Text
        If txt <> cc.PlaceholderText.Value Then   'ignore placeholder text
            newVal = MapListValue(txt)            'get the new list entry value
            Debug.Print txt, le.Value, newVal
            If currVal = txt Then Set sel = le    'remember which was selected, if any
            le.Text = newVal                      'you may not need to set both?
            le.Value = newVal
        End If
    Next le
    If Not sel Is Nothing Then sel.Select 're-select any selected option
End Sub


'Map a list entry to a new value: return original value if no mapping
Function MapListValue(v)
    Select Case v
        Case "OriginalValue_1": MapListValue = "NewValue_1"
        Case "OriginalValue_2": MapListValue = "NewValue_2"
        Case "OriginalValue_3": MapListValue = "NewValue_3"
        Case "OriginalValue_4": MapListValue = "NewValue_4"
        Case "OriginalValue_5": MapListValue = "NewValue_5"
        Case Else: MapListValue = v 'just return original value
    End Select
End Function