Paragraph Index

E

EGR

I am trying to return the index of a parapgraph within a
document. What I would like to do is initiate a string
search and when the string is found I would like to
return the index value of the paragarph within the
paragraph collection for that document. I am interested
in doing this so that I can also return the following
paragraph assuming it exists.
 
J

Jay Freedman

Hi, EGR,

As that article says, "The first question is, why do you need to know
the index number?" You can get the text of the next paragraph, if any,
by using a range and a paragraph object, as in the following example,
without ever knowing the index of the paragraph:

Sub FindAndReturnNextPara()
Dim MyRg As Range
Dim MyPara As Paragraph

Set MyRg = ActiveDocument.Range
With MyRg.Find
.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.Text = "find me"
If .Execute Then
Set MyPara = MyRg.Paragraphs(1)
If Not (MyPara.Next Is Nothing) Then
Set MyRg = MyPara.Next.Range
MsgBox MyRg.Text
Else
MsgBox "Found text in the last paragraph."
End If
End If
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