Hello Paul,
After much testing, I found that in order to have the users still user their prior forms created in Word 2002, I mush use the find method in my code as Word 2002 does not support rich text controls. I will have the user run a macro that will update the 2002 documents to include the necessary fields; however, they will not be controls. I created the below macro that first determines if there are controls, if yes then it’s a new form and I use the code you provided. If no, then it’s a prior version of the form and the code uses the find method. The problem I am having is that this is not working by section. The text found from the first section using the find method shows up on each section using the same method. When I read the code it appears to me that it is only running by sections… but somehow is it not stopping after each section. Any ideas
? A big thanks for any insight you can provide!
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim strTmp As String, oCel As Cell, rngTmp As Range, i As Integer, j As Integer
With ActiveDocument
If .Sections.Count > 5 Then
For i = 6 To .Sections.Count
With .Sections(i).Range
If .ContentControls.Count > 3 Then
strTmp = .ContentControls(2).Range.Text
For j = 3 To .ContentControls.Count
strTmp = strTmp & ":" & .ContentControls(j).Range.Text
If j > 3 Then Exit For
Next j
If Not .ContentControls(5).PlaceholderText = .ContentControls(5).Range.Text Then
strTmp = strTmp & .ContentControls(5).Range.Text
End If
For Each oCel In .Tables(1).Range.Cells
Set rngTmp = oCel.Range
rngTmp.End = rngTmp.End - 1
If rngTmp.Text = vbNullString Then
ActiveDocument.Indexes.MarkEntry Range:=rngTmp, Entry:=strTmp & employment & """" & " \f " & """" & "TOC", _
CrossReference:="", CrossReferenceAutoText:="", _
BookmarkName:="", Bold:=False, Italic:=False
Exit For
End If
Next oCel
ElseIf .ContentControls.Count = 0 Then
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Category 1:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
strTmp = Selection.Text
Selection.Collapse
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Category 2:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCell
strTmp = strTmp & ":" & Selection.Text
Selection.Collapse
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Category 3:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCell
strTmp = strTmp & ":" & Selection.Text
Selection.Collapse
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Category 4:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCell
strTmp = strTmp & ":" & Selection.Text
Selection.Collapse
For Each oCel In .Tables(1).Range.Cells
Set rngTmp = oCel.Range
rngTmp.End = rngTmp.End - 1