How can I use an OR in the Find and Replace

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Very simply, I want to find either "shall" or "requires". I've tried every
variation of a regular expression i could think of to make it work. Help
would be very appreciative!!
 
Shawn,

To the best of my knowledge it can't be done as you would expect.
Another user and I where discussing using OR in a find text problem. The
discussion turned to a very real human situation where the house is locked
and the car is locked. A known spare house key is in the car and a known
spare car hey is in the house. There are other keys somewhere to be found.
Obviously the problem is resolved and the search can end when either a house
or car key is found.


Sub HelmutsDilemma()
Dim aWord As Range
Dim myStr As String
For Each aWord In ActiveDocument.Range.Words
myStr = aWord
If InStrRev(myStr, " ") = Len(myStr) Then
myStr = Left(myStr, (Len(myStr) - 1))
End If
Select Case myStr
Case Is = "car", "house"
If aWord.Next(Unit:=wdWord, Count:=1) = "keys " Or _
aWord.Next(Unit:=wdWord, Count:=1) = "keys" Then
With aWord
.MoveEnd Unit:=wdWord, Count:=1
.Select
End With
Exit Sub
End If
Case Else
'Do Nothing
End Select
Next
End Sub
 
Very simply you can't. You can perform two separate searches or use a macro.
Greg has suggested one possibility another would be as follows. The macro
finds 'shall' and replaces it with WORD1 then 'requires' and replaces it
with WORD2.

Sub ReplaceList()
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long
vFindText = Array( "shall", "requires")
vReplText = Array("WORD1", "WORD2")
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute replace:=wdReplaceAll
Next i
End With
End Sub

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
"=?Utf-8?B?U2hhd24gU2hhbm5vbg==?=" <Shawn
(e-mail address removed)> wrote in
Very simply, I want to find either "shall" or "requires". I've tried
every variation of a regular expression i could think of to make it
work. Help would be very appreciative!!

Others have advised you that's it not possible in Word.

There is another software (Copernic Desktop Tool) which offers use of the
OR option and some other unique search options.
Copernic is highly configurable for file types (most every file type on
your computer is included), sizes and folders.

You may download it for free:
http://www.copernic.com/

The search help options (
http://help.copernic.com/topic/desktopsearch15en/boolean.htm )

The software works by building and index of the files on your computer.
I do however, CAUTION you to reduce the file size limit from the default of
60meg to something reasonable (I changed it to 2meg), otherwise you'll be
days watching your computer build a working index for the software.

I have a very large quanity of RTF and text files stored and I'm really
not sure how I functioned without this software previously.
It saves me a trememdous amount of time.

NO, I'm not a hawker for Copernic.
It's just a remarkable tool that I use.
Previously the closet thing I could find was a software named Odyssey:
http://www.sorcerersoftware.com/odyssey.htm

Copernic puts Odyssey too shame.
 
Graham,

Don't really know why, but sometimes your code run here would miss the
second word. I made some changes and it seems to be working all the time
now.

Sub ReplaceList()
Dim oRng As Word.Range
Dim oSelection As Word.Range
Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

Set oRng = ActiveDocument.Range
Set oSelection = Selection.Range
vFindText = Array("shall", "requires")
vReplText = Array("WORD1", "WORD2")
With oRng.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = 0 To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = vReplText(i)
.Execute Replace:=wdReplaceAll
Next i
End With
oSelection.Select
End Sub
 
It would be interesting to know why the line
.MatchWholeWord = True
doesn't work in either version ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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

Back
Top