Originally Posted by
heedaf
I'm using a range.find to find a specific word in a paragraph and I need to get the first 3 words before and the 3 words after.
So why not simply move the start & end without computing the index?
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Text"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.Execute
End With
If .Find.Found Then
.MoveStart wdWord, -6
While .ComputeStatistics(wdStatisticWords) > 4
.MoveStart wdWord, 1
Wend
.MoveEnd wdWord, 6
While .ComputeStatistics(wdStatisticWords) > 7
.MoveEnd wdWord, -1
Wend
MsgBox .Text
End If
End With
Application.ScreenUpdating = True
End Sub