Defining Range for Find or Setting A Counter Variable

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
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Use:

Dim myrange As Range, myword As Range, i As Long, Counter As Long
With ActiveDocument.Range
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
End With
For i = 1 To ActiveDocument.Sections.Count - 1
Set myrange = ActiveDocument.Sections(i).Range
Counter = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="protest", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set myword = Selection.Range
If myword.InRange(myrange) = True Then
Counter = Counter + 1
End If
Loop
End With
ActiveDocument.Range.InsertAfter "Section " & i & " contains the
word 'protest' " & Counter & " times." & vbCr
Next i


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
C

Culichi

Wow. Thanks for the extended effort in the response, Doug. I'll try that
out. I appreciate your time!!

Kelley


Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS
wrote:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top