Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Paragraph Index Oddity (Word 2000)

    Here's an odd one.

    I have a routine that - for reasons that need not be gone into -- steps, using Paragraph(Index), from the current paragraph to the nearest/previous heading 3, then steps from the same current paragraph to the previous instance of the same style, and then from that paragraph to it's nearest/previous heading 3.(Basically, I'm determining if Instance1 and Instance 2 of StyleA are beneath the same heading or is Instance 2 under heading-1)

    Enough backstory. I've found that when I run the routine that does this, stepping, say from parapgraph 700 to paragraph 690, it takes 1 second. When I do the same thing at the end of a document, using the same code, with a starting paragraph index of, say, 3000, it takes 5 seconds (which is way too long to make the solution usable).

    The attached template has an example module name InsertCont, in which there is a test procedure called TestProblem. If you open the attached template and open the VBE and run TestProblem you'll get the following debug.print results:

    page 30, paragraph 731
    3:29:33 AM
    3:29:34 AM
    page 109, paragraph 2688
    3:29:34 AM
    3:29:39 AM

    Any idea why this is happening? Seems to me the slowdown is in the While Wend loop, not the GetParagraphIndex -- it's as if every time the Range is being set to a paragraph VBA is counting from the beginning fo the text stream.
    Attached Files Attached Files

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    I've seen such behavior even back in the daze of WordBasic.

    THe deeper into the document, the longer some things take.

    Never invesigated, but could be due to repagination taking longer as you get deeper, or Word updating internal structures depending on where you are in the doc.

    And, using the paragraphs collection for such a purpose is very inefficient. Try using the EXpand method to navigate, it makes a very big difference, especially when used with the Range object.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    Have you looked at the source code? I don't think the expand method will work.

    The point is not to navigate from here to there or the expand the selection from here to there, but to analyze the styles preceding the IP (without moving the IP as far at the user is concerned).

    And repaginatoin shouldn't be happening (and doesn't seem to be happening) since the IP is not moving and text is not being added or view changed.

    A previous version of this module used EditFind to locate the previous instances of current style and to determine whether the two paragraphs were under the same heading three or separated by a heading 3.

    The problem with that method is that all the movement, bookmark dropping, would make the active window jump on the screen.

    The advantage of the Paragraph(index).Style method (at least in the first half of a long document -- and the documents I'm working with average 120 pages -- was it was screen quiet and fast.

    I just find it odd that their should be any difference between:
    <pre>Debug.Print ActiveDocument.Paragraphs(20).Style

    and

    Debug.Print ActiveDocument.Paragraphs(2000).Style
    </pre>

    Thanks, though. Looks like it's back to the drawing board.

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    The IP does not move with the Range object.
    Expand works well with Range.

    Use the Range Object with Find, IP does not move.

    Using Range is faster than using Paragraph object in every instance I've seen.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    <P ID="edit" class=small>Edited by ggallo on 13-Jun-02 02:26.</P>Took me a while to figure out what you were suggesting... but thanks. It looks like a combo of range.find and a few paragraph(index) calls will do what I want.

  6. #6
    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: Paragraph Index Oddity (Word 2000)

    This change in the function chops the total time down significantly, but doesn't erase the overall discrepancy:
    <pre>Function GetPrevHeadContent(sty, i)
    <font color=blue>Dim aRange As Range</font color=blue>
    Set aRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(i).Range.End - 1, _
    End:=ActiveDocument.Paragraphs(i).Range.End)
    While aRange.Style <> sty
    i = i - 1
    <font color=448800>'Set aRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(i).Range.End - 1, _
    End:=ActiveDocument.Paragraphs(i).Range.End)</font color=448800>
    <font color=blue>aRange.Move wdParagraph, -1
    aRange.MoveStart wdCharacter, -1</font color=blue>
    Wend
    GetPrevHeadContent = ActiveDocument.Range(Start:=ActiveDocument.Paragra phs(i).Range.Start, _
    End:=ActiveDocument.Paragraphs(i).Range.End - 1)
    End Function</pre>

    As shown in the following table, this seems to highlight the problem in the While...Wend loop in the main procedure. Maybe you can try the range.move/range.movestart strategy for that loop, too?

    <table border=1><td>Before</td><td>After</td><td><pre>page 30, paragraph 731
    ...before getPara: 0.000
    ...before GetPrevious: 0.008
    ...before WhileWend: 0.547
    [Index Starts @ 731]
    [Index Ends @ 715]
    ...before GetPrevious: 0.852
    Top done: 0.859

    page 109, paragraph 2688
    ...before getPara: 0.883
    ...before GetPrevious: 0.945
    ...before WhileWend: 2.523
    [Index Starts @ 2688]
    [Index Ends @ 2672]
    ...before GetPrevious: 3.656
    Bottom done: 3.727</pre>

    </td><td><pre>page 30, paragraph 731
    ...before getPara: 0.000
    ...before GetPrevious: 0.023
    ...before WhileWend: 0.063
    [Index Starts @ 731]
    [Index Ends @ 715]
    ...before GetPrevious: 0.352
    Top done: 0.375

    page 109, paragraph 2688
    ...before getPara: 0.383
    ...before GetPrevious: 0.453
    ...before WhileWend: 0.586
    [Index Starts @ 2688]
    [Index Ends @ 2672]
    ...before GetPrevious: 1.711
    Bottom done: 1.773</pre>

    </td></table>
    Attached Files Attached Files

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    Thanks, Jefferson.

    I will give some thought on how to dispense with the While Wend --

    What I'm doing now is combining the current version of this (which stepped backwards through paragraphs) and the older version (which used EditFind) -- using the tip Howard supplied about range.find not moving the IP.

    Seems to be getting there.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    U do not need the paragraphs collection if you use Expand. Just keepp your own counter as Expand moves the Range. It is very much faster than using the paragraphs collection.

    Also, tho harder to program, using the Range object usually yields cleaner code.

    For those of you who have seen my musing/srants/etc. over the past 9 years or so, you may recall I periodically mentioned a shareware project I was working on, tho I was not sure I'd live long enough to finish the critter

    Started it in Word 7, ended up with abot 19000 lines of WordBasic code that also ran in VBA. A couple of years ago, I decided to can the WB and convert entirely to VBA. Well, I've done that (with the exception of 1 statement I've been too lazy to convert, and a small number that have no VBA equivalent).

    Well, the critter is now just under 21000 lines and I am deep into the process of purifying the code, i.e., removing inferior WB based algorithms and changing code to use Range Object rather than Selection object.

    Code improvement is amazing!

    Now if I can only live long enough to release the darn software!

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    For those of you who may have been following this discusion, the results are in.

    Inspired by suggestions here, I've totally revised how I'm stepping around my document. The time improvement is quite impressive.

    <table border=1><td>Latest</td><td>p30</td><td>0.031</td><td>p109</td><td>0.063</td><td>Scher Mod</td><td>p30</td><td>0.188</td><td>p109</td><td>2.766</td><td>Original</td><td>p30</td><td>1.672</td><td>p109</td><td>6.828</td></table>

    I'm attaching RangeSolution.dot for anyone interested in seeing how it was done. Might be useful.

    Open VBE and execute TestIt.

    Thanks to all.
    Attached Files Attached Files

  10. #10
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    Hi Guy,

    Another possibility might be
    <pre>Dim sng As Single
    sng = Timer()
    ActiveDocument.Bookmarks("Test2").Select
    Selection.GoTo What:=wdGoToBookmark, Name:="HeadingLevel"
    MsgBox Timer - sng</pre>


    That selects the current subchapter, and should be too fast to be measurable with the Timer.
    Working with the Selection isn't always all that bad...

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    Hi, Klaus,

    I think that would select from heading 1 down, no?

    In any case, it doesn't solve the issure of having to find the previous instance of a none heading level style (same as the current paragraph's style).

    And I think it would have the same disadvantage of my original code -- screen flicker as the selection is moved and moved back.

    The solution working with the range seems to be working so far

  12. #12
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    > I think that would select from heading 1 down, no?

    No. It selects everything from the previous heading up to the next heading of the same (or higher) level.

    > And I think it would have the same disadvantage of my original code -- screen flicker as the selection is moved and moved back.

    If the macro takes 0.0X seconds, screen flicker shouldn't matter that much...

    > The solution working with the range seems to be working so far

    Didn't want to criticize that solution; I didn't quite understand your original problem, and only wanted to mention an alternative approach to work with chapters/subchapters.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    Another way to improve performance is to NOT use bookmarks, unless the bookmark needs to be saved with the doc.

    For example, instead of:

    ActiveDocument.Bookmarks.add name:="Whatever", Range:=Selection.Range

    Use

    Set rngWhatever = Selection.Range

    And then use rngWhatever instead of th bookmark. Has at least 2 benefits:

    1. Adding a bookmark dirties a document and Word then asks you to save the document even if tere are really no changes to the doc.

    2. Greatly reduces the need for the Selection object AND the Select method. For examplem instead of using the Select method to go to a bookmark, you can just reference the range. Makes a big difference.

  14. #14
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    The current solution doesn't use bookmarks.

  15. #15
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Paragraph Index Oddity (Word 2000)

    You don't really need to work with the Selection when using Bookmarks:
    Set myRange = ActiveDocument.Bookmarks("here").Range
    (... but using Ranges only is probably much faster, as you said. On the other hand, Bookmarks are safer if your macro inserts or deletes stuff: Ranges easily get clobbered then).

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

Page 1 of 2 12 LastLast

Posting Permissions

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