Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding Current Paragraph Number

Posted on 2001-07-16
4
Medium Priority
?
477 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

618 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