Solved

Finding Current Paragraph Number

Posted on 2001-07-16
4
461 Views
Last Modified: 2008-02-01
I'm looking for a VBA only solution, this needs to be incorporated into existing code. Either an encapsulated function to return the code or a snippet that can be included into existing is fine.

The problem is I need to know my current insertion point's paragraph number, to deal with some formatting. Why I need it is not the best reason in the world, but alternatives are nastier. What I can't find though, is a way to find my current paragraph number location.

Already Attempted:
Locating an index in selection.paragraphs or trying to match selection.paragraph against the indexed collection. You can only pull the collection out of the selection.

Using selection.information, for the way you'd typically find the page number/row count/yadayada, they didn't include the paragraph # index within this.

Coming at it through a count using a bookmark to store orig location, then a selection extend and counting paragraphs, is starting to appear to be the only way out of it, and I can build this already. Does anyone else have a direct method to pull the information or am I building a workaround?

Thanks in advance.

-Craig
0
Comment
Question by:Gudare
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
blakeh1 earned 100 total points
ID: 6287091
Sub GetParaNum()
    Dim rSelection As Range
    Dim r As Range
    Dim p As Paragraph
    Dim i As Long
    Set rSelection = Selection.Range
    i = 1
    For Each p In ActiveDocument.Paragraphs
        Set r = p.Range
       
        If rSelection.InRange(r) Then
            MsgBox "Selection in paragraph " & i
        End If
        i = i + 1
    Next
End Sub
0
 
LVL 2

Author Comment

by:Gudare
ID: 6287110
Thanks, Blakeh1, but as I said, I have a workaround. More is always better, though. Unfortunately, I'm trying to locate something that doesn't require a loop (document is rather large, picture how many pages x paragraphs, per usage).

What I'd really like is some undocumented feature (Hell, hand me a bug that returns the wrong information or something. :)) that basically ejects the current paragraph number back to you, or a non-looping method to *quickly* find paragraph #8983 down on page 385, if that's where I'm forced to end up.

Although, at least now I have a solution that's not going to mess with other formatting and the need for bookmarks.

-Craig
0
 
LVL 2

Author Comment

by:Gudare
ID: 6287801
Thanks for the answer. The original workaround I was using wasn't as efficient as this due to the bookmarking not working as expected in 2002. Instead of arguing with it, I'm going to use this method and dump out on a successful find. Thanks.
0
 
LVL 6

Expert Comment

by:blakeh1
ID: 6289961
here is another way that doesn't involve looping

Sub GetParaNum()
    Dim r As Range
    Dim r2 As Range
    Set r = Selection.Range
    Selection.MoveEnd wdParagraph, 1
    'Collapse selection to the end and then select from that
    'point to the start of the document. Then the paragrpah number
    'will be equal to the selection's paragraph count
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
   
    Set r2 = Selection.Range
    MsgBox r2.Paragraphs.Count
   'Make sure to return to the original selection
    r.Select
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now