Hi Dave,
How could I find the text and then display a message
box like: " 'sample text' is located in paragraphs 7 and 22 of this
document, which is 34 paragraphs in total." ?
First, a messagebox to display the result the way you suggested,
is not a too good idea. Some people here are processing very long
documents, and I doubt, whether a messagebox would work the way you
like, if there were some 100 successful finds, even too many for the
direct window (direct area ? ) or whatever it is called in English.
Have a look at this:
---
Sub test440()
Dim rDcm As Range ' range document
Dim rTmp As Range ' range to count paragrphs in
Dim iPrg As Integer ' a counter for paragraphs
Dim iFnd As Integer ' a counter for successful finds
Set rDcm = ActiveDocument.Range ' initializing range
Set rTmp = ActiveDocument.Range ' initializing range
Resetsearch
With rDcm.find
.Text = "wxvy"
While .Execute
iFnd = iFnd + 1
rTmp.End = rDcm.End
' expand rtmp to end of found position
iPrg = rTmp.Paragraphs.Count
Debug.Print "wxvy in paragraph " & iPrg
Wend
End With
MsgBox "found in " & iFnd & " paragraphs"
Resetsearch
End Sub
'---
Sub Resetsearch()
With Selection.find
.Parent.Collapse
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub