Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Counting References/paragraphs of a given style??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ML - 08 Sep 2006 17:39 GMT
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
Jay Freedman - 08 Sep 2006 18:33 GMT
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
ML - 08 Sep 2006 21:03 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.