Re: Defining Range for Find or Setting A Counter Variable

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL ADDRESS (dkr_at_mOSTvALUABLEpROFESSIONALs.org)
Date: 03/01/04


Date: Mon, 1 Mar 2004 15:21:43 +1000

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
"Culichi" <kdstrawn@students.wisc.edu> wrote in message 
news:4042386C.5050202@students.wisc.edu...
>
> 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
> 


Relevant Pages

  • Defining Range for Find or Setting A Counter Variable
    ... Happy Leap Day to you all-- ... hits to be inserted at the bottom of the document so I can later convert ... it's searching the whole document, then the whole document minus the ...
    (microsoft.public.word.vba.beginners)
  • Re: Are there any man pages for any of the system error messages?
    ... Searching for the whole phrase/sentence/message, ... with quotation marks around it, Google returns _FOUR_ hits. ... you searched for the phrase. ...
    (comp.os.linux.misc)
  • Re: Foxtrot, Tuesday, October 11th
    ... with + and/or put them in quotes, your results will be filtered more ... So, for example, searching for "out the damned ... whether he is washing out the damned spot. ... whole lot more hits, as you can verify for yourself. ...
    (rec.arts.comics.strips)
  • Re: Xerofoam or Xeroform gauze
    ... off as a helpful source if you storm in with the entitlement. ... One of our 'members' wrote something helpful about Google searching, ... If it's teaching me to learn how to "fish" or research properly, ... mean -- if there are more hits does that mean that it's legit or not? ...
    (sci.med.transcription)
  • Re: "shred" as a past participle
    ... Searching for "could ... [snip some speculation] ... My understanding is that the various local Google URLs are all merely ... I don't want to quibble about a mere six hits ...
    (alt.usage.english)