The problem is that when you have two or more consecutive paragraphs with
the same My Reference style, the Find.Execute considers them to be only one
"found" instance. That means the variable l never exceeds the document's
paragraph count, so the loop never stops.
Since this approach doesn't work too well, try this instead:
Sub CountReferenceParas()
Dim aPara As Paragraph
Dim nRefs As Long
nRefs = 0
For Each aPara In ActiveDocument.Paragraphs
If aPara.Style = "My Reference" Then
nRefs = nRefs + 1
End If
Next
MsgBox "found " & nRefs & " references"
End Sub

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
> We currently have a macro to loop through a document and cound
> "references" which are marked with a specifc style so that we can
[quoted text clipped - 12 lines]
> endloop:
> ActiveDocument.CustomDocumentProperties("RefCount") = l
Klaus Linke - 08 Sep 2006 20:03 GMT
Another method, which might be faster on long documents: search for
paragraph marks ^p in that style.
http://www.word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm
Regards,
Klaus
"Jay Freedman" <jay.freedman@verizon.net> schrieb:
> The problem is that when you have two or more consecutive paragraphs with
> the same My Reference style, the Find.Execute considers them to be only
[quoted text clipped - 33 lines]
>> endloop:
>> ActiveDocument.CustomDocumentProperties("RefCount") = l
Turned out that this line was the issue:
ActiveDocument.Range.Paragraphs.Count
Seems calling this in the loop took an extremely long amount of time and
perhaps has a memory leak. Called it once before the loop and set a
variable and it worked fine.
> We currently have a macro to loop through a document and cound
> "references" which are marked with a specifc style so that we can find
[quoted text clipped - 13 lines]
> endloop:
> ActiveDocument.CustomDocumentProperties("RefCount") = l
Jay Freedman - 09 Sep 2006 01:51 GMT
That must mean that none of your "My Reference" paragraphs are located
together, but are all separated by paragraphs with other styles. If
you ever run the macro on a document that does have consecutive
reference paragraphs, you'll see a real infinite loop, not just a slow
one.
I suggest you take Klaus's advice before that happens.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
>Turned out that this line was the issue:
>ActiveDocument.Range.Paragraphs.Count
[quoted text clipped - 20 lines]
>> endloop:
>> ActiveDocument.CustomDocumentProperties("RefCount") = l