View Single Post
 
Old 08-30-2017, 01:12 AM
sylvio sylvio is offline Windows 7 64bit Office 2010 32bit
Novice
 
Join Date: Jan 2017
Posts: 20
sylvio is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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
Reply With Quote