bad karma with loop?

L

Lara

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
 
D

Dave Lett

Hi Lara,

I don't know the answer to your question, but I know that it can be a little
easier and faster than with the code you're using. Have a look at the
article "How to find out, using VBA, how many replacements Word made during
a Find & Replace All" at
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm

Especially review the section in that article on "How to find out how many
occurrences there are of a particular word in a document ".

HTH,
Dave
 
P

Peter Hewett

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
 
D

Dave Lett

Hi all,

FYI and FWIW, at the end of Dave Rado's article "How to find out, using VBA,
how many replacements Word made during a Find & Replace All" at
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm, he writes that
"although it is possible to achieve the same ends by using a counter while
you do multiple Finds (or multiple Find & Replaces) one at a time, until
nothing more is found, that method is much slower than doing a ReplaceAll as
illustrated above."
 
P

Peter Hewett

Hi Dave Lett

I'm aware of that technique. I deliberately posted this version as it employs
the same principals as the OPs original code.

Cheers - Peter

Hi all,

FYI and FWIW, at the end of Dave Rado's article "How to find out, using VBA,
how many replacements Word made during a Find & Replace All" at
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm, he writes that
"although it is possible to achieve the same ends by using a counter while
you do multiple Finds (or multiple Find & Replaces) one at a time, until
nothing more is found, that method is much slower than doing a ReplaceAll as
illustrated above."

HTH + Cheers - Peter
 

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