search only the words within a selection

J

JenFruels

Hi,
Does anybody know how to search for a certain word in a specific
selection/highlighted text/paragraph of a document only? I have tried
recording a macro but I was not able to get what I want. The following code
will highlight the paragraph/portion where the enclosing bookmark is located.
In that highlighted/selected area, the macro will search for the word
"hello". I only want the macro to find those which are only within the
selection. I know this is possible when using the Search & Replace Dialog box
but I can't make it run on macro. Below is my sample code.I can't figure out
what's missing. I am using Word 2003. Thanks.

Sub LinkTest()
Dim a As String
Selection.GoTo What:=wdGoToBookmark, Name:="tmp"
With Selection.Find
.text = "hello"
End With
Do
a = Selection.Find.Execute
If a = True Then
MsgBox (Selection.text)
End If
Loop While a = True
End Sub
 
G

Graham Mayor

While your macro starts by selecting the range, the selection is cancelled
with the first find. You need to maintain the range in order to continue to
search it eg

Sub LinkTest()
Dim a As String
Dim oRng As Range
Selection.GoTo What:=wdGoToBookmark, name:="tmp"
Set oRng = Selection.Range
oRng.Find.Text = "hello"
Do
a = oRng.Find.Execute
If a = True Then
MsgBox (Selection.Text)
End If
Loop While a = True
End Sub

As written, the message box will show the content of the range.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jean-Guy Marcil

JenFruels said:
Hi,
Does anybody know how to search for a certain word in a specific
selection/highlighted text/paragraph of a document only? I have tried
recording a macro but I was not able to get what I want. The following code
will highlight the paragraph/portion where the enclosing bookmark is located.
In that highlighted/selected area, the macro will search for the word
"hello". I only want the macro to find those which are only within the
selection. I know this is possible when using the Search & Replace Dialog box
but I can't make it run on macro. Below is my sample code.I can't figure out
what's missing. I am using Word 2003. Thanks.

Sub LinkTest()
Dim a As String
Selection.GoTo What:=wdGoToBookmark, Name:="tmp"
With Selection.Find
.text = "hello"
End With
Do
a = Selection.Find.Execute
If a = True Then
MsgBox (Selection.text)
End If
Loop While a = True
End Sub

If all you want is a message box, then this will do the trick:

Dim oRng As Range

Set oRng = ActiveDocument.Bookmarks("tmp").Range

With oRng.Find
.Text = "hello"
Do While .Execute
MsgBox (.Parent.Text)
Loop
End With


However, if you want to do something to the found text, try this instead:

Dim oRng As Range

Set oRng = ActiveDocument.Bookmarks("tmp").Range
With oRng.Find
.Text = "hello"
Do While .Execute
With .Parent
.HighlightColorIndex = wdPink
.Font.Size = 16
End With
Loop
End With


BY the way, personally, I would not code like this:

Dim a As String
Selection.GoTo What:=wdGoToBookmark, Name:="tmp"
(Snip)
Do
a = Selection.Find.Execute
If a = True Then
(Snip)

"a" is declared as a String, but ".Execute" returns a Boolean... I do not
like to mismatch the variable types...

Since I know that ".Execute" returns a boolean, I do not even need a
variable since that Boolean value is not needed outside the loop.
 
J

JenFruels

Hi Jean
Thanks for the advice. =) I was confused why it was not working. I would
like to find the words with a certain style. As I used the range, it still
keeps on prompting me the phrases having that style, even it is not within
that bookmark range. Thanks.

Sub LinkTest()
Dim oRng As Range
Set oRng = ActiveDocument.Bookmarks("ChapSep2").Range
With oRng.Find
.text = ""
.Style = "Chap affil"
.MatchWildcards = False
Do While .Execute
MsgBox (.Parent.text)
Loop
End With
 
J

Jean-Guy Marcil

JenFruels said:
Hi Jean
Thanks for the advice. =) I was confused why it was not working. I would
like to find the words with a certain style. As I used the range, it still
keeps on prompting me the phrases having that style, even it is not within
that bookmark range. Thanks.

Sub LinkTest()
Dim oRng As Range
Set oRng = ActiveDocument.Bookmarks("ChapSep2").Range
With oRng.Find
.text = ""
.Style = "Chap affil"
.MatchWildcards = False
Do While .Execute
MsgBox (.Parent.text)
Loop
End With

Try this variation then:


Dim oRng As Range
Dim lngRangeEnd As Long

Set oRng = ActiveDocument.Bookmarks("ChapSep2").Range
lngRangeEnd = oRng.End

With oRng.Find
.Text = ""
.Style = "Chap affil"
Do While .Execute
If .Parent.Start > lngRangeEnd Then Exit Do
MsgBox (.Parent.Text)
Loop
End With
 

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