Re: bad karma with loop?
From: Peter Hewett (nospam_at_xtra.co.nz)
Date: 04/09/04
- Next message: Peter Hewett: "Re: Restrict input to an input box to numerical format"
- Previous message: Greg Maxey: "Restrict Input Box input to numerical format"
- In reply to: Lara: "bad karma with loop?"
- Next in thread: Dave Lett: "Re: bad karma with loop?"
- Reply: Dave Lett: "Re: bad karma with loop?"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Peter Hewett: "Re: Restrict input to an input box to numerical format"
- Previous message: Greg Maxey: "Restrict Input Box input to numerical format"
- In reply to: Lara: "bad karma with loop?"
- Next in thread: Dave Lett: "Re: bad karma with loop?"
- Reply: Dave Lett: "Re: bad karma with loop?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|