Log in

View Full Version : Stop search at end of document



norgro
07-28-2012, 05:09 AM
I am writing a Word macro to repeat a search until the end of the document is reached. At each find tests are performed.

I have used selection.end to give a character count that I use to determine the end of the document. I can stop the search when it returns to the beginning of the document.

Is there a better way to stop the search once it reaches the end of the document?

gmaxey
07-28-2012, 06:01 AM
You rarely need to use selection with find.

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Test"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
Debug.Print oRng.Text
Wend
End With
End Sub

norgro
07-28-2012, 11:53 PM
Thank you for responding to my request for help.

I am still having a problem. I guess I did not give enough information.

In Braille there is a contraction for "ea" and a contraction for "ar". If "ea" is followed by "r", the "ar" contraction takes precedence. So for each "ea" that is found in the text being transcribed, I need to check if the next character is an "r" before highlighting the "ea".

I have tried, unsuccessfully, to insert code between the While.Execute and the Wend, to determine if the next character is "r".

I am highlighting contractions in text passages to assist with the teaching of Braille for the sighted! I am a volunteer at the Association for the Blind Western Australia. At age 82, my VBA skills are a bit rusty!

Any further suggestions would be appreciated.

Norm

gmaxey
07-29-2012, 05:58 AM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "ea"
.Wrap = wdFindStop 'stops at the end of the document
While .Execute
If oRng.Characters.Last.Next = "r" Then
'Do nothing
Else
'Highlight the "ea"
oRng.HighlightColorIndex = wdYellow
End If
Wend
End With
End Sub

norgro
07-29-2012, 04:55 PM
Thanks again Greg. You have made my day! I really appreciate your help.
Norm