Word VBA >> Infinite Loop with a find and replace

by S29rb21vam8 » Thu, 19 Feb 2009 23:40:07 GMT

Hi, I'm wondering if someone would be kind enough to help me. I'm working
with a script and want to highlight different character names and their
dialogue. I can highlight the character names easily with Find and Replace.
But their subsequent dialogue, in a new paragraph under their character
names, is proving more difficult. I've created a macro that will do this,
but when it reaches the end of the script, it asks me if I want to continue
searching at the top of the document. If I say yes, it loops infinitely; if
I say no, it keeps asking again and again, another loop. Here's my code,
below. Any help is appreciated. Thanks.


Sub HighlightEllie()
'
' HighlightEllie Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
Options.DefaultHighlightColorIndex = wdYellow

With Selection.Find
.Text = "ELLIE^p"
.Replacement.Text = "ELLIE^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
End With


Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting

With Selection.Find
.Text = "ELLIE^p"
.Replacement.Text = "ELLIE^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
End With


Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting

Do While True

With Selection.Find
.Text = "ELLIE^p"
.Forward = True
.Wrap = wdFindAsk
.MatchCase = True
End With


Selection.Find.Execute


Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow
Selection.HomeKey Unit:=wdLine

Loop


End Sub



Word VBA >> Infinite Loop with a find and replace

by Helmut Weber » Fri, 20 Feb 2009 00:51:01 GMT


Hi Kokomojo,

it looks to me as if you wanted to highlight "Ellie" at the end of
a paragraph and highlight the following paragraph, too.
If so, you don't need more than the following:

Sub Test5555()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "ELLIE^p"
While .Execute
rDcm.HighlightColorIndex = wdYellow
' prevent error if ELLIE is at the end of the last paragraph
If Not rDcm.Paragraphs(1).Next Is Nothing Then
rDcm.Paragraphs(1).Next.Range.HighlightColorIndex = wdYellow
End If
Wend
End With
End Sub

I think also, that
leads to an endless loop,
if the actual paragraph is the last paragraph in the doc.
Then
Selection.MoveDown has nothing to move to,
but doesn't error. It just does nothing.

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP



Word VBA >> Infinite Loop with a find and replace

by S29rb21vam8 » Fri, 20 Feb 2009 02:18:02 GMT

Thanks Helmut, this worked like a charm!





Similar Threads

1. replace header text: get section data in header to halt infinite loop VBA - Word VBA

2. Help on .NET Word Object Library Range Find.Execute infinite Loop

3. Infinite loop - Microsoft Office

4. Infinite loop in Groove 3.1

I've moved a large number of files to new workspaces, to unload some old 
workspaces.  Altogether I moved maybe 1 gig.  Everything looked fine until I 
opened up Groove after the move and there was a great number (maybe 800+ mg) 
of files to be uploaded again.  Since then it's been a week or so and now the 
constant number I see every day is 105 mg.  How do I get this transfer to 
stop uploading, and where are all these files going?  Thanks.

5. Infinite loop when using track changes - MS Word Support

6. Infinite Loop Problem

I had a professor contact me today about a problem he was having with a word 
document. For some reason he entered something then hit enter and a character 
Y with two little dots just started appearing on the screen. He managed to 
close word down but now when ever you open up this document it sits in an 
infinite loop and locks word.

I was wondering if there is any tool available to analyze this file to see 
if it can be fixed. If someone wants a copy of the file to check out the 
problem please ask.

Eric

7. infinite loop in winword.exe - Microsoft Word

8. StrReverse() function - infinite loop

I have just started program with Word 2002
While trying to reverse each word in a line of text encountered a 
problem: Word enters an infinite loop and stops responding.
Could somebody explain why?
Code:
---------------
Sub TestReverseWords()
Dim myRange As Range
Dim aWord As Object
Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.End)
For Each aWord In myRange.Words
     aWord.Text = StrReverse(aWord.Text)
Next aWord
End Sub
--------------
end code

Thanks in advance.
Mike