View Single Post
 
Old 08-29-2017, 07:21 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

Thank you, it works as expected for a single title of CCs.
When I try to adapt the code for an array of names (I need to check about 20 differently named CCs and their duplicates) using cycle For ... Next, the result is dissapointing.
For example, for two CCs named "A" with contents "1" and "2" and CCs named "B" with contents "3" and "4" the result is CCs "A" with content "12" and "B" with "3, 412". And it is expect "A" with "1, 2" and "B" with "3, 4".
What is wrong?

Here is the code I have used:

Code:
Sub Demo()
Dim i, j As Long, StrTtl As String, StrTxt As String, CCnames
CCnames = Array("A", "B"): 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))
        StrTxt = Replace(StrTxt, "|", ", ")
        .Text = Trim(.Text) & StrTxt
        
      End With
    
   End With
Next
Reply With Quote