Solved

Finding Current Paragraph Number

Posted on 2001-07-16
4
466 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

730 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