How to use Edit Find to select everything from where the cursor is to the first found item
Article contributed by Ibby
Turn on “Extend” mode, do the Find, and turn “Extend” mode off again.
If doing this manually, you can turn “Extend” mode on by pressing F8, and you can turn it off by pressing Esc. Or you can double-click in the status bar, where it says EXT, both to switch it on and to switch it off again (it will be greyed out unless “Extend” mode is on).
As well as text you can use this to find and select to a particular Style, or a special character, or whatever you like.
Doing it programmatically
If doing it programmatically, use something like the following:
Application.ScreenUpdating = False
' Turn on ExtendMode
Selection.ExtendMode = True
' Perform the search
With Selection.Find
.ClearFormatting
.Text = "fox"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found
Then
MsgBox "The selection has been
extended."
Else
MsgBox "Search word not found."
End If
End With
' Turn off ExtendMode
Selection.ExtendMode = False
Application.ScreenUpdating = True
Warning: If doing it programmatically, and if you plan to follow up by doing further Find operations within the resulting selection, stick to using Selection.Find. Otherwise, timing problems sometimes seem to result.
Or alternatively, use Range.Find throughout and don't use ExtendMode at all, as in the following example:
Dim
MyRange As Range, StartRange
As Range
Set MyRange = Selection.Range
MyRange.Collapse wdCollapseStart
Set StartRange = MyRange.Duplicate
With MyRange.Find
.Forward = True
.Wrap = wdFindStop
.Text = "fox"
.Replacement.Text = ""
.Execute
If .Found
Then
''Extend the
range from the found item back to the start of the original range
MyRange.Start =
StartRange.Start
Set
StartRange = MyRange.Duplicate
' Within
myRange, change some text.
With
MyRange.Find
.Forward =
True
.Wrap =
wdFindStop
.Text =
"brown"
.Replacement.Text
= "red"
.Execute
Replace:=wdReplaceAll
End With
End If
End With
If using Word 97, Selection.Find is much faster than Range.Find (this bug was fixed in Word 2000); so if any of your users are using Word 97, the former method (using ExtendMode and sticking to Selection.Find throughout) is probably the best idea.