Results 1 to 8 of 8
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Para Collection Index (Word 2000)

    With the insertion point in a paragraph of a document, is there a way to return the value of the Index of that paragraph in the Paragraphs Collection? For example, I would like to determine if the cursor is in the last paragraph of the document, or it it's in the 3rd paragraph (index = 2), I might want to delete the previous paragraph. Knowing the index value would be a help.

    Thanks.
    Richard Barrett

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Para Collection Index (Word 2000)

    Richard,

    I don't think there's any quick, clean way to do that.
    Offhand, I think you'd probably have to loop through all the paragraphs in the document with a counter, testing each paragraph as you go along, to see if the selection is in that paragraph - at which point you would exit the loop - the current value of the counter would be your index.
    - That's a pretty slow process just to find out the index number of a paragraph... <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

    Gary

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Much Faster Method (Word 2000)

    Hi again,

    A coupla interesting things came up in trying to get this code to work: one was that the various Selection .Type and .Information properties didn't do the trick (had to use "Selection.InRange").

    The other may be of broader usefulness so here goes:
    It's been documented before that this method:

    For Each aPara in ActiveDocument.Paragraphs
    apara.DoSomething
    Next 'aPara

    is approx. 100 times faster than this method:

    For n = 1 to ActiveDocument.Paragraphs.Count
    ActiveDocument.Paragraphs(n).DoSomething
    Next 'n

    because looping through the collection via an index is (apparently) internally less efficient.

    Since you needed to get an index number, the initial thought was that you need to use the latter, slower method:

    <pre>Public Sub MuchSlowerGetIndexOfSelectedParagraph()
    Dim n As Long
    Dim DocParas As Paragraphs
    Set DocParas = ActiveDocument.Paragraphs
    For n = 1 To DocParas.Count
    If Selection.InRange(DocParas(n).Range) Then
    Exit For
    End If
    Next 'n
    MsgBox "Selection is in paragraph: " & n
    Set DocParas = Nothing
    End Sub</pre>

    - the above took 35 seconds to run on a long test document on my PC.

    The following code uses the For Each/Next, and implements a separate counter to keep track of the index:

    <pre>Public Sub MuchFasterGetIndexOfSelectedParagraph()
    Dim n As Long
    Dim aPara
    Dim DocParas As Paragraphs
    Set DocParas = ActiveDocument.Paragraphs

    For Each aPara In DocParas
    n = n + 1
    If Selection.InRange(aPara.Range) Then
    Exit For
    End If
    Next 'aPara
    MsgBox "Selection is in paragraph: " & n
    Set DocParas = Nothing
    End Sub
    </pre>

    - this code took 1 second on the same long test doc - much much faster.

    Gary

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Without the iterations

    I thought maybe a non-iterative way would also be quick. Select back to the beginning of the document and count how many paragraphs are selected. Using ranges would look something like this.
    <pre>Dim aRange As Range, pos, endpos
    pos = ActiveDocument.Paragraphs(1).Range.Start
    endpos = Selection.Range.End
    Set aRange = ActiveDocument.Range(Start:=pos, End:=endpos)
    MsgBox aRange.Paragraphs.count</pre>

    You need to also work out if the endpos is better as a start of selection rather than the end (relevant if something is actually selected) and also if the cursor is at the start of a paragraph then that para doesn't get counted in this method.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Without the iterations

    Very cool - ranges rule! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Without the iterations

    Now why did I know you would be happier if I didn't use the selection object. Of course it's harder to record the bits I needed using ranges but sometimes its worth it just to see your reaction.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Without the iterations

    Who was it that showed me this one-line version a while ago? Was that you Andrew?

    Function ParagraphNumber() As Integer
    'This gives you the index number of the current paragraph
    ParagraphNumber = ActiveDocument.Range(Start:=0, End:=Selection.Paragraphs(1).Range.End).Paragraphs .Count
    End Function
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Without the iterations

    Kevin

    As Bart says - It wasn't me, Nobody saw me, You can't prove a thing.

    I don't know the source but that won't stop me shamelessly plagiarising it as I will find a use for the method one day.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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