Solved

Finding Current Paragraph Number

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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…

747 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

8 Experts available now in Live!

Get 1:1 Help Now