Quote:
Originally Posted by macropod
What is wrong is that you're not resetting StrTxt to "|" on each iteration of the loop
|
Many thanks!
Now the code works as expected
Code:
Sub Demo()
Dim i, j As Long, StrTtl As String, StrTxt As String, CCnames
CCnames = Array("A", "B", "C"): StrTxt = "|"
For j = 0 To UBound(CCnames)
With ActiveDocument
On Error Resume Next
For i = .SelectContentControlsByTitle(CCnames(j)).Count To 2 Step -1
With .SelectContentControlsByTitle(CCnames(j))(i)
If InStr(StrTxt, "|" & Trim(.Range.Text) & "|") > 0 Then
.Range.Text = vbNullString
.Delete
ElseIf .ShowingPlaceholderText = True Then
.Delete
Else
StrTxt = "|" & Trim(.Range.Text) & StrTxt
.Range.Text = vbNullString
.Delete
End If
End With
Next
With .SelectContentControlsByTitle(CCnames(j))(1).Range
StrTxt = Replace(StrTxt, "|" & Trim(.Text), "")
StrTxt = Left(StrTxt, Len(StrTxt) - 1)
StrTxt = Replace(StrTxt, "|", ", ")
.Text = Trim(.Text) & StrTxt
StrTxt = "|"
End With
End With
Next
End Sub