Results 1 to 13 of 13
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    whats the current paragraph index (OfficeXP SP3)

    HI

    I have a very large document. My insertion point has is at a particular paragraph in the document. How do I retun the current paragraph(index) so I can then work with this paragraph?

    diana

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: whats the current paragraph index (OfficeXP SP

    You can use the current paragraph with
    Selection.Paragraphs(1).Range

    but you can find the actual document paragraph index with
    ParagraphNumber = ActiveDocument.Range(Start:=0, End:=Selection.Paragraphs(1).Range.End).Paragraphs .count

    This solution came from <post#=144525>post 144525</post#>

    I seem to remember a cleaner way to get the document paragraph index but I couldn't remember what to search with so I didn't find it.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    thanks andrew that helped me...many thanks diana :-)

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Hi Andrew,

    Here's a more generalized function that uses only the paragraph without using the ActiveDocument object:

    <pre>Function GetParagraphIndex(para As Paragraph) As Long
    GetParagraphIndex = para.Range.Document.Range(0, para.Range.End).Paragraphs.Count
    End Function
    </pre>


  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Andrew77

    Do you forsee a problem in using the ActiveDocument object? How would you envisage a para getting passed in without relying on the selection which infers the ActiveDocument anyway.

    <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> Congratulations on getting the book published (and on getting Audrey's name in print and on the web at the same time - multiple brownie points there)
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: whats the current paragraph index (OfficeXP SP

    I may be wrong, but I believe some of my procedures work with text in non-active documents. By using Ranges, I believe you can, for example, write code that cuts or copies text from the Active Document and pastes it into another document without having to "activate" the 2nd document (or vice versa). So in that type of situation the more generic paragraph-index function could come in handy.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Meanwhile, many thanks to Andrew77 for showing me that the Range object has a Document property. I've been using Parent, and I've been a little worried that that might lead to the wrong result if I ran one of those macros when the relevant Range was in a footer or something. Also, if you type Parent and a dot, IntelliSense always refuses to troop along beyond that point.

  7. #7
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Hi Andrew,

    Steve's right on -- with multiple documents open, it might happen that the current "ActiveDocument" was not the same document that contatined the para passed to the function. While it wouldn't often be a problem, it would be tricky to pin down when it did happen (the code probably wouldn't fail, you'd just get an incorrect index value).

  8. #8
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Yeah, that's always bugged me about Intellisense as well.

    You won't always be able to use Document directly from Range (it depends whether you're working with the Range property or a Range object), but in my experience Parent always returns the correct Document object, even if you're in a footer, etc.

    Cheers!

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Hi Andrew77

    Yes, I agree that ActiveDocument is not always going to be relevant but with my limited imagination I can't imagine not knowing enough about the para object to need to find its absolute index unless I was using the selection object. If I know how to specify the para so I can pass it to the function then what new information is the absolute paragraph index going to yield that I didn't already have?

    I don't see a real world scenario where I would actually use the function if it didn't involve the selection object and the ActiveDocument. It would come in handy if I ever needed to count how many paragraphs in a document before the first table or graphic but what other situations would you actually use this function.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Hi Andrew,

    I've got a template used by book authors to write manuscripts in. Each chapter is its own file, but authors can create dynamic cross references among the chapters using INCLUDETEXT fields and bookmarks (all hidden behind custom dialogs).

    There's a custom dialog to allow users to create references to paragraphs that aren't included in the standard reference types in the Word xref dialog, and that dialog uses the paragraph offset for one reason or another. At any rate, at one point, the code needs the paragraph index of the reference target, which is not in the currently active document:

    <pre>...
    iBookmarkParaIndex = GetParaIndex(docTarget.Bookmarks(sBookmarkName).Ra nge.Paragraphs.First)
    ...
    </pre>


    Also, I do a lot of batch processing, and I've occasionally found the ActiveDocument object to be unpredictable. So even on the rare occasions that I use the Selection object, I get to the active document using Selection.Document. But maybe I'm just paranoid.

    I was just offering my version as a fully generalizable function, not dependent on the state of the current selection or document.

    Cheers,

  11. #11
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Thanks Andrew

    I see your imagination is much better than mine. <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22> I haven't had to go that far with cross-referencing as yet although I once considered creating an addin to allow you to do this sort of cross-references between documents with a right click drag'n'drop rather than fuss with building and populating user forms to do the same thing.

    I have run into ActiveDocument being unpredictable but not yet when using the selection object.

    Thanks for the additional information, I can see where you get mileage from the function now.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  12. #12
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the current paragraph index (OfficeXP SP

    I have found "undocumented features" in Word that severely interfered with reliable use of the "ActiveDocument" object. The best way that I found to deal with the idea of ActiveDocument, without relying on Word to remember which document is/was active, is to do something like the following:

    Sub MySub()
    Dim myDoc As Document
    Set myDoc = Documents.Open(some_document.doc)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: whats the current paragraph index (OfficeXP SP

    Just one small note:

    > you can't (and shouldn't) use the Selection object with this approach

    If the document is not hidden, you can, if you insert an additional property/object so that the Selection tracks specifically to myDoc -- myDoc.ActiveWindow.Selection -- but just because you can doesn't mean you should. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> It still may be more efficient to use Range objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •