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

Re: search only the words within a selection by Graham

Graham
Thu Apr 10 03:16:53 PDT 2008

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


JenFruels wrote:
> 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



RE: search only the words within a selection by JeanGuyMarcil

JeanGuyMarcil
Thu Apr 10 04:46:01 PDT 2008

"JenFruels" wrote:

> 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.

RE: search only the words within a selection by JenFruels

JenFruels
Mon Apr 14 02:51:03 PDT 2008

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


RE: search only the words within a selection by JeanGuyMarcil

JeanGuyMarcil
Mon Apr 14 08:51:01 PDT 2008

"JenFruels" wrote:

> 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


RE: search only the words within a selection by JenFruels

JenFruels
Mon Apr 14 19:46:00 PDT 2008

Wow, great! This is what I'm looking for, thanks Jean! =)