Re: bad karma with loop?

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Peter Hewett (nospam_at_xtra.co.nz)
Date: 04/09/04


Date: Fri, 09 Apr 2004 13:01:14 +1200

Hi Lara

TRy the following code it's what I use:

Sub FindAndCount()
    Dim rngReplace As Word.Range
    Dim lngCount As Long

    Set rngReplace = ActiveDocument.Content
    With rngReplace.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<trid:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False

        ' Find all occurrences in the document
        Do While .Execute
            lngCount = lngCount + 1

            ' Resume the search after the text that we just found
            rngReplace.Collapse wdCollapseEnd
        Loop
    End With

    MsgBox lngCount & " occurrences were found"
End Sub

HTH + Cheers - Peter
 

"Lara" <lava@mindspring.com>, said:

>I use this Count Occurrences public sub with modifications
>in several VBA macros I've written (see below). For some
>reason, locks up sometimes--here's the pattern, hoping
>someone might be able to help--if I run the macro
>consisting solely of this count occurrances loop (below),
>it runs fine as many times as I care to run it, in
>different documents, etc. BUT if I run one of my longer
>macros containing it, then I have to close Word and reopen
>it or it locks up if I try to run any macro (including the
>basic one, below) containing the loop. Is there
>some "interaction" I'm missing here or is this
>random "Word bad karma" or what? Something that would
>make this screwy if run after/before it? Any idea of a
>fix/work-around much appreciated!
>It locks up (tested by having it print msgboxes with each
>step as it loops) after it counts the last occurrance in
>the document, after it exits the loop and on the end with
>line (it prints a msgbox after the loop line the final
>time for the count, but then locks up.
>
>Public Sub CountOccurrences()
>'modified by Lara
>Dim strSearch As String
>strSearch = "<trid:"
>icount = 0
>With ActiveDocument.Content.Find
> .Text = strSearch
> .Format = False
> .Wrap = wdFindStop
> Do While .Execute
> icount = icount + 1
> Loop
> End With
>MsgBox Chr$(34) & strSearch & Chr$(34) & " was found " & _
> icount & " times."
>End Sub



Relevant Pages

  • RE: Email and Fax a Report
    ... Opening a report in a loop seems odd, ... By placing the sub on its own, ... Dim objOutlook As Outlook.Application ... Set newMail = objOutlook.CreateItem ...
    (microsoft.public.access.modulesdaovba)
  • Re: Recognizing When An IE window changes URLs and closing the IE
    ... lpClassName As String, ByVal lpWindowName As String) As Long ... Private Sub x_NewWindow2 ... Dim ie1 As New IEClass ... On Error GoTo Here ' Error handling sequence that breaks the loop, ...
    (microsoft.public.excel.programming)
  • Re: Deleting cells in a row
    ... Sub Loop_Example ... Dim Firstrow As Long ... 'We loop from Lastrow to Firstrow ... If I understand you correct you only want to delete cells in the row with no formula. ...
    (microsoft.public.excel.programming)
  • Re: Problem with timer control
    ... the loop varies ... The timer code below basically does this same ... >Private Sub MyTask1() ... >Dim string1 As String ...
    (microsoft.public.vb.controls)
  • Re: Getting There...Almost!!!
    ... of the inner loop is the better approach in my opinion. ... Tom Ogilvy ... >> Public Sub remove ... Dim lastrow As Long ...
    (microsoft.public.excel.programming)