Select x words on either side of Find

A

Anachostic

Here's an interesting request. I'm just looking for the most efficient way to handle this.

I have a need to do a find and replace on a text range. The replace is simply a formatting change. However, I need to change the formatting on a specified number of words on either side of the matched word as well.

I am looking at the object model, but figured maybe someone who knows it a bit better could point me right where I needed to go.

My anticipated approach is to match the word, step back x words, start selecting, step forward ((x*2)+1)) words, end selecting, then apply the formatting to the selection. That doesn't sound like the best way to do it.

Maybe I could do a regular expression match? Any other ideas?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Anachostic > écrivait :
In this message, < Anachostic > wrote:

|| Here's an interesting request. I'm just looking for the most efficient
way to handle this.
||
|| I have a need to do a find and replace on a text range. The replace is
simply a formatting
|| change. However, I need to change the formatting on a specified number
of words on either side
|| of the matched word as well.
||
|| I am looking at the object model, but figured maybe someone who knows it
a bit better could
|| point me right where I needed to go.
||
|| My anticipated approach is to match the word, step back x words, start
selecting, step forward
|| ((x*2)+1)) words, end selecting, then apply the formatting to the
selection. That doesn't sound
|| like the best way to do it.
||
|| Maybe I could do a regular expression match? Any other ideas?

Something like this maybe:

'_______________________________________
Sub FindBeforeAfter()

Dim oRange As Range
Dim FindWord As String
Dim EitherSide As Long

Set oRange = ActiveDocument.Range
FindWord = "your word"
'how many words on either side, if the number is different
'do not use the variable and hard code it
EitherSide = 2

With oRange.Find
.ClearFormatting
.Text = FindWord
.Forward = True
.Wrap = wdFindStop
Do While .Execute
With oRange
.MoveStart wdWord, -EitherSide
.MoveEnd wdWord, EitherSide + 1
.Font.Bold = True
.Collapse wdCollapseEnd
.End = ActiveDocument.Range.End
End With
Loop

End With

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Anachostic > écrivait :
In this message, < Anachostic > wrote:

|| Very very cool. I never would've written it as efficiently as that!
||
|| Thank you!
||

You're welcome!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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