Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get a Paragraph's Index (2003)

    I got so sick of trying to remember the syntax for doing this, that I put it in a function a while ago. Perhaps others can also get some use out of it.
    <pre>Function GetParagraphIndex(para As Paragraph) As Long
    GetParagraphIndex = para.Range.Document.Range(0, para.Range.End).Paragraphs.Count
    End Function
    </pre>

    It returns the index of a paragraph, for example:

    <pre>Sub WhatIsIndexOfSelectedParagraph()
    MsgBox GetParagraphIndex(Selection.Range.Paragraphs.First )
    End Sub
    </pre>


    Enjoy!

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Get a Paragraph's Index (2003)

    The answer is out there if you know how to search for it. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Amendment: Your version improves on those that use ActiveDocument, since the target document might not always be what Word considers the ActiveDocument.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Get a Paragraph's Index (2003)

    Very interesting thread, and a great example of watching the group think out a problem. You may know I'm a big fan of the For Each loop, and <post#=113868>post 113868</post#> is yet another example of why.

    BTW, You can use a similar trick with the Selection object:
    <pre>MsgBox Selection.Document.Range(0, _
    Selection.Range.Paragraphs.First.Range.End).Paragr aphs.Count
    </pre>

    Not pretty, but shows how you can "walk" the object hierarchy from the bottom up, and back down again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •