I have document with two paragraph: in the first one one Plain text CC and in the second one repeating section CC with one Plain Text CC inside.
I am trying to move all the info from CC's in repeating section to appear in first paragraph CC. So far I have come to this code:
Sub first_paragraph()
Dim x As Integer
Dim xNames As String
Dim xEntry As String
xNames = ""
With ActiveDocument.SelectContentControlsByTag("VP_pav")
For x = 1 To .Count
With .Item(x)
If .Tag = "VP_pav" Then
xEntry = ActiveDocument.SelectContentControlsByTag("VP_pav").Item(x).Range.Text
' "if" for removing comma before first entry
If xNames = "" Then
xNames = xEntry + xNames
Else
xNames = xEntry + ", " + xNames
End If
End If
End With
Next x
End With
ActiveDocument.SelectContentControlsByTag("pirm_pas").Item(1).Range.Text = xNames
End Sub
Problem that CC generated in repeating section CC (then adding new section) seems to get random item number (I thought that first CC with tag "example" would get item number 1, second CC with the same tag get item number 2 and etc., but it seems it is not the case).
Because newly generated CC gets random item numbers, values in first paragraph CC are out of order.
Is it possible to make newly generated CC to get sequential numbers? How they get item numbers if they are not sequential? Should take some different approach?
I am adding picture to illustrate my situation:
P.S. I'm deletting my recently posted question about correct variables order in this situation since I determined that it is (probably) not the problem.
With the code shown, nothing is telling Word to read the content controls in any kind of order. In this case, a better approach would be to loop the content controls in the Repeaing Section content control.
The following example assumes the RepSec
has been assigned to the Title
of the repeating section content control:
Sub first_paragraph()
Dim xNames As String, xEntry as String
Dim ccRepSec as Word.ContentControl
Dim cc as Word.ContentControl
xNames = ""
Set ccRepSec = ActiveDocument.SelectContentControlsByTitle("RepSec").Item(1)
For Each cc in ccRepSec.Range.ContentControls
If .Tag = "VP_pav" Then
xEntry = cc.Range.Text
If xNames = "" Then
xNames = xEntry & xNames
Else
xNames = xEntry & ", " & xNames
End If
End If
Next cc
ActiveDocument.SelectContentControlsByTag("pirm_pas").Item(1).Range.Text = xNames
End Sub