C
Culichi
Happy Leap Day to you all--
Could someone please help me try to figure out what needs to be tweaked
in the code below? I've got a Word (XP) document that has dozens of
sections in it, each section is an article to be searched. I want to
count the number of times a specific word ("protest" in this example)
occurs in each article (defined as a section). I then want the total
hits to be inserted at the bottom of the document so I can later convert
them to a table. The code below works fine when there is no hit in the
section (returns a 0), but returns a descending number of hits for other
sections. For example, let's say there are 100 hits in the whole
document, but none in the first three. The string of numbers at the end
would look something like:
0
0
0
100
0
0
97
0
93
Etc.
So, either I'm defining the selected range for the sections wrong and
it's searching the whole document, then the whole document minus the
first section, then the whole minus the second, etc.; Or, I am not
successfully reseting the counter. Because the numbers start high, I
figure it's the first explanation.
Apologies for the length. Many thanks if you can help. I suspect it's
something very simple. Here's the code:
'Make identify each section of the document as a variable
'in the Sections Collection
Dim MySections As Section
'Do the specified search for each section of the document
For Each MySections In ActiveDocument.Sections
'Set each section as a range to be searched
MySections.Range.Select
'Search
With Selection.Find
Dim ProtCount As Long
Set ProtCount = 0
.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
'If section search is False, return a 0
If .Execute(FindText:="protest") = False Then
'Record 0 at the end of the document
ActiveDocument.Content.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.TypeText Text:="0"
.InsertParagraphAfter
End With
'If section search is True, count how many True
Else
Do While .Execute(FindText:="protest") = True
ProtCount = ProtCount + 1
Loop
'Record # of hits at the end of the document
ActiveDocument.Content.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.InsertAfter (ProtCount)
.InsertParagraphAfter
End With
End If
End With
Next
Could someone please help me try to figure out what needs to be tweaked
in the code below? I've got a Word (XP) document that has dozens of
sections in it, each section is an article to be searched. I want to
count the number of times a specific word ("protest" in this example)
occurs in each article (defined as a section). I then want the total
hits to be inserted at the bottom of the document so I can later convert
them to a table. The code below works fine when there is no hit in the
section (returns a 0), but returns a descending number of hits for other
sections. For example, let's say there are 100 hits in the whole
document, but none in the first three. The string of numbers at the end
would look something like:
0
0
0
100
0
0
97
0
93
Etc.
So, either I'm defining the selected range for the sections wrong and
it's searching the whole document, then the whole document minus the
first section, then the whole minus the second, etc.; Or, I am not
successfully reseting the counter. Because the numbers start high, I
figure it's the first explanation.
Apologies for the length. Many thanks if you can help. I suspect it's
something very simple. Here's the code:
'Make identify each section of the document as a variable
'in the Sections Collection
Dim MySections As Section
'Do the specified search for each section of the document
For Each MySections In ActiveDocument.Sections
'Set each section as a range to be searched
MySections.Range.Select
'Search
With Selection.Find
Dim ProtCount As Long
Set ProtCount = 0
.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
'If section search is False, return a 0
If .Execute(FindText:="protest") = False Then
'Record 0 at the end of the document
ActiveDocument.Content.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.TypeText Text:="0"
.InsertParagraphAfter
End With
'If section search is True, count how many True
Else
Do While .Execute(FindText:="protest") = True
ProtCount = ProtCount + 1
Loop
'Record # of hits at the end of the document
ActiveDocument.Content.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.InsertAfter (ProtCount)
.InsertParagraphAfter
End With
End If
End With
Next