?
Solved

Finding Current Paragraph Number

Posted on 2001-07-16
4
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
blakeh1 earned 300 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

801 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