• Get a Paragraph’s Index (2003)

    Author
    Topic
    #406178

    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.

    Function GetParagraphIndex(para As Paragraph) As Long
        GetParagraphIndex = para.Range.Document.Range(0, para.Range.End).Paragraphs.Count
    End Function
    

    It returns the index of a paragraph, for example:

    Sub WhatIsIndexOfSelectedParagraph()
        MsgBox GetParagraphIndex(Selection.Range.Paragraphs.First)
    End Sub
    

    Enjoy!

    Viewing 1 reply thread
    Author
    Replies
    • #840306

      The answer is out there if you know how to search for it. smile

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

      • #840411

        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 is yet another example of why.

        BTW, You can use a similar trick with the Selection object:

        MsgBox Selection.Document.Range(0, _
                     Selection.Range.Paragraphs.First.Range.End).Paragraphs.Count
        

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

      • #840412

        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 is yet another example of why.

        BTW, You can use a similar trick with the Selection object:

        MsgBox Selection.Document.Range(0, _
                     Selection.Range.Paragraphs.First.Range.End).Paragraphs.Count
        

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

    • #840307

      The answer is out there if you know how to search for it. smile

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

    Viewing 1 reply thread
    Reply To: Get a Paragraph’s Index (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: