location of text in Word

D

Dave B

Can someone point me towards a book/website that describes how the Word
object model works?

I'm trying to get the location of "sample text" in a Word document using the
Word's Find method. 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." ?

Thanks for your help.
 
H

Helmut Weber

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
 

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