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?

Re: Select x words on either side of Find by Jean-Guy

Jean-Guy
Sat Jun 26 09:27:28 CDT 2004

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org




Re: Select x words on either side of Find by Anachostic

Anachostic
Sat Jun 26 16:43:35 CDT 2004

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

Thank you!


"Jean-Guy Marcil" wrote:

> 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
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>
>
>
>

Re: Select x words on either side of Find by Jean-Guy

Jean-Guy
Sat Jun 26 19:40:43 CDT 2004

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org