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 them. The issue
is that this code seems to be hanging Word and not sure why. Does anyone
have any ideas on what the issue is or a better way to handle this?

With ActiveDocument.Range.Find
.Style = "My Reference"
While .Execute
l = l + 1
If l > ActiveDocument.Range.Paragraphs.Count Then
GoTo endloop
End If
Wend
End With
endloop:
ActiveDocument.CustomDocumentProperties("RefCount") = l

Re: Counting References/paragraphs of a given style?? by Jay

Jay
Fri Sep 08 12:33:20 CDT 2006

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

--
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.

ML wrote:
> 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 them. The issue is that this code seems to be hanging Word and
> not sure why. Does anyone have any ideas on what the issue is or a
> better way to handle this?
> With ActiveDocument.Range.Find
> .Style = "My Reference"
> While .Execute
> l = l + 1
> If l > ActiveDocument.Range.Paragraphs.Count Then
> GoTo endloop
> End If
> Wend
> End With
> endloop:
> ActiveDocument.CustomDocumentProperties("RefCount") = l



Re: Counting References/paragraphs of a given style?? by Klaus

Klaus
Fri Sep 08 14:03:20 CDT 2006

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
> 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
>
> --
> 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.
>
> ML wrote:
>> 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 them. The issue is that this code seems to be hanging Word and
>> not sure why. Does anyone have any ideas on what the issue is or a
>> better way to handle this?
>> With ActiveDocument.Range.Find
>> .Style = "My Reference"
>> While .Execute
>> l = l + 1
>> If l > ActiveDocument.Range.Paragraphs.Count Then
>> GoTo endloop
>> End If
>> Wend
>> End With
>> endloop:
>> ActiveDocument.CustomDocumentProperties("RefCount") = l
>
>



Re: Counting References/paragraphs of a given style?? by ML

ML
Fri Sep 08 15:03:08 CDT 2006

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.

"ML" <schooner@accesswave.ca> wrote in message
news:u4qpwV20GHA.1568@TK2MSFTNGP03.phx.gbl...
> 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
> them. The issue is that this code seems to be hanging Word and not sure
> why. Does anyone have any ideas on what the issue is or a better way to
> handle this?
>
> With ActiveDocument.Range.Find
> .Style = "My Reference"
> While .Execute
> l = l + 1
> If l > ActiveDocument.Range.Paragraphs.Count Then
> GoTo endloop
> End If
> Wend
> End With
> endloop:
> ActiveDocument.CustomDocumentProperties("RefCount") = l
>



Re: Counting References/paragraphs of a given style?? by Jay

Jay
Fri Sep 08 19:51:57 CDT 2006

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.

On Fri, 8 Sep 2006 17:03:08 -0300, "ML" <schooner@accesswave.ca>
wrote:

>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.
>
>"ML" <schooner@accesswave.ca> wrote in message
>news:u4qpwV20GHA.1568@TK2MSFTNGP03.phx.gbl...
>> 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
>> them. The issue is that this code seems to be hanging Word and not sure
>> why. Does anyone have any ideas on what the issue is or a better way to
>> handle this?
>>
>> With ActiveDocument.Range.Find
>> .Style = "My Reference"
>> While .Execute
>> l = l + 1
>> If l > ActiveDocument.Range.Paragraphs.Count Then
>> GoTo endloop
>> End If
>> Wend
>> End With
>> endloop:
>> ActiveDocument.CustomDocumentProperties("RefCount") = l
>>
>