Sears two words near each other

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

Guest

Is there a way to search a Word document for word1 near word2? It would be
nice to be able to specify what "near"means (e.g., same sentnece, same
paragraph, within n words).
 
The following macro will find and select the text beginning and ending with
the words that it asks for if they are in the same paragraph. I could be
modified for the other cases that you mention.

Dim myrange As Range
Dim firstword As String
Dim secondword As String
Dim i As Long
firstword = InputBox("Enter the first word that you want to find")
secondword = InputBox("Enter the second word that you want to find")
i = Len(secondword)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=firstword, MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range
myrange.End = Selection.Paragraphs(1).Range.End
If InStr(myrange, secondword) > 1 Then
myrange.End = myrange.Start + InStr(myrange, secondword) + i - 1
myrange.Select
Exit Do
Else
Selection.Collapse wdCollapseEnd
End If
Loop
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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

Similar Threads

Data cleanup 1
Rules 1
Let's all buy from Sears 2
Parse textbox words for query values 5
Meta tag syntax 6
Arrays have me confused 6
Finding words near other words 5
SQL CE: Speed improvement? 5

Back
Top