Find this OR that

L

Lerxst

Hi. Is there a way--I presume by using wildcards--to, for
example, find 'cat' or 'dog'? I guess I'm asking whether
Word will allow me to do a Boolean type search for this
term OR that term.

P.S. I'm not interested in a solution that would involve
me using the VBScript Regular Expressions object. (Not
that I have anything against it; I'm just trying to find
out whether it can be done in Word's find dialog.)

Thank you.
Lerxst
 
K

Klaus Linke

Lerxst said:
Hi. Is there a way--I presume by using wildcards--to, for
example, find 'cat' or 'dog'? I guess I'm asking whether
Word will allow me to do a Boolean type search for this
term OR that term.

P.S. I'm not interested in a solution that would involve
me using the VBScript Regular Expressions object. (Not
that I have anything against it; I'm just trying to find
out whether it can be done in Word's find dialog.)

Thank you.
Lerxst


Hi Lerxst,

There's nothing built-in ... not with the built-in Find dialog.
For fun, I just wrote a macro to find the next occurrance of any word in
some list (see below).

Regards,
Klaus


Sub SearchWords()
' Best create a keyboard shortcut to call this macro.
' See Bill Coan's
' http://word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToHotkey.htm
' list of words to search for:
Dim Words As Variant
' remember last search list for future searches:
Dim myVar As Variable
Dim strOld As String
strOld = ""
' to test if such a list exists:
Dim boolOldExists As Boolean
boolOldExists = False
' Found match:
Dim iPosFound As Long
Dim iLenFound As Long
' Current "nearest" match:
Dim iPos As Long
Dim iLen As Long
' Rememer old selection:
Dim rngSelection As Range
' some loop counter:
Dim i As Long

For Each myVar In ActiveDocument.Variables
If myVar.Name = "FindWords" Then
strOld = myVar.Value
boolOldExists = True
End If
Next myVar
If boolOldExists = False Then
ActiveDocument.Variables.Add Name:="FindWords", Value:=" "
End If
Words = _
InputBox("Find next occurrance of any of these words:", _
"Find words", ActiveDocument.Variables("FindWords").Value)
ActiveDocument.Variables("FindWords") = Words
Words = Split(Words, " ")
Set rngSelection = Selection.Range
iPos = rngSelection.Start
For i = 0 To UBound(Words)
Selection.Find.ClearFormatting
With Selection.Find
.Text = Words(i)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
iPosFound = Selection.Start
iLenFound = Len(Words(i))
' Is this a better match than the previous?
' Case 1: There is a match below the current selection
If iPos > rngSelection.Start Then
If (iPosFound < iPos) And (iPosFound > rngSelection.Start) Then
iPos = iPosFound
iLen = iLenFound
End If
' Case 2: The current best match is above the current selection
Else
If (iPosFound > rngSelection.Start) Or (iPosFound < iPos) Then
iPos = iPosFound
iLen = iLenFound
End If
End If
' Set selection back to original:
rngSelection.Select
End If
Next i
If iPos <> rngSelection.Start Then
ActiveDocument.Range(Start:=iPos, End:=iPos + iLen).Select
Else
rngSelection.Select
End If
End Sub
 
K

Klaus Linke

In case someone wants to test/use the macro:
You need to put
ActiveWindow.ScrollIntoView Selection.Range, True
after
ActiveDocument.Range(Start:=iPos, End:=iPos + iLen).Select

I thought .Select would automatically scroll the view to the Selection, but
it doesn't :-(

Klaus
 
L

Lerxst

Thanks for your effort. I threw together something
similar to what you did in the meantime. Pretty handy.

Lerxst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top