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 / February 2005

Tip: Looking for answers? Try searching our database.

Macro which does not stop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry - 15 Feb 2005 13:53 GMT
I have a macro (pasted below) that searches for any
occurance of 2 styles in a row. In this case the
style "term". It then highlights the ones that it finds so
ic an easily spot it. The problem being is that when the
last style in the document is "term", the macro does not
stop. I am sure I am missing something basic here.

Thanks for your help

Sub XMLDoubleTerm()
Dim oRng As Range
With Selection
   .HomeKey Unit:=wdStory
   With .Find
       .text = ""
       .Style = "term"
       Do While .Execute
           Set oRng = Selection.Range
           With oRng
               Do Until .Paragraphs.Last.Range.Style
<> "term"
                   .MoveEnd Unit:=wdParagraph, Count:=1
               Loop
                   .MoveEnd Unit:=wdParagraph, Count:=-1
               If .Paragraphs.Count > 1 Then
                   .HighlightColorIndex = wdYellow
               End If
           End With
       Loop
   End With
End With
End Sub
Greg - 15 Feb 2005 15:32 GMT
Jerry,

The continous loop was due to the lines:

Do Until .Paragraphs.Last.Range.Style <> "term"
  .MoveEnd Unit:=wdParagraph, Count:=1
               Loop

Try the following to break out of that loop:

Sub XMLDoubleterm()
Dim oRng As Range
Dim i As Long
i = ActiveDocument.Paragraphs.Count
With Selection
   .HomeKey Unit:=wdStory
   With .Find
       .Text = ""
       .Style = "term"
       Do While .Execute
           Set oRng = Selection.Range
           With oRng
               Do Until oRng.End = ActiveDocument.Range.End Or
.Paragraphs.Last.Range.Style <> "term"
                   .MoveEnd Unit:=wdParagraph, Count:=1
               Loop
                   If oRng.End < ActiveDocument.Range.End Then
                     .MoveEnd Unit:=wdParagraph, Count:=-1
                   End If
               If .Paragraphs.Count > 1 Then
                   .HighlightColorIndex = wdYellow
               End If
           End With
       Loop
   End With
End With
End Sub

Rate this thread:






 
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.