Results 1 to 6 of 6
  1. #1
    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

    Range.Find bugginess (Word 2002 SP-2)

    I have an elaborate macro that loops through a (former WordPerfect) document multiple times to find various double-bracketed strings and then performs various formatting actions when it finds them. The macro works perfectly in its original version, but I've started to rewrite some of the code to work as much as I can with the Range.Find object rather than the Selection.Find object. For the most part, this is working, but some of the rewritten searches cause Word to freeze, forcing me to use Task Manager to shut it down.

    The freezes always seem to happen in the immediate wake of a Range.Find that came up empty. Where it's a simple Find loop that is causing the freeze and the document has instances of the target, the code runs fine until it performs the final .Execute that returns False. Where it's a ReplaceAll version of .Execute, the code runs fine unless there are no instances of the target in the document.

    The freezes are erratic only in the sense that much of my rewritten Range.Find code is working fine (including when the search comes up empty). But the freezes are consistent in the sense that a particular Range.Find that freezes in a particular document will always freeze at that same point in that same document (including if I'm stepping through the code).

    Has anyone else experienced this kind of bugginess with Range.Find in Word 2002?

    (P.S. I've tried throwing in DoEvents and pauses using Timer, but these haven't had any effect.)

    (P.P.S. I should add that these are not "converted" WordPerfect documents. I'm copying the WordPerfect documents' contents (stripped of all codes) to the clipboard and pasting them into Word as unformatted text -- so I don't think the WordPerfect origin has anything to do with the Range.Find problem.)

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Range.Find bugginess (Word 2002 SP-2)

    Does it help if you test whether the Range object has become Nothing?

    If rngMyRange Is Nothing Then
    ' Something has gone wrong, either warn user
    ' or set the range again
    Else
    ' OK, continue
    End If

  3. #3
    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: Range.Find bugginess (Word 2002 SP-2)

    Bugs in Range.Find?? A few have crawled in here before. Not sure if any of these threads help:

    Odd Word Find/Replace Behavior (Word 2000 SR-1)
    Searching within a range (WORD 97/2000)

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

    Re: Range.Find bugginess (Word 2002 SP-2)

    Sorry, this probably is obvious to you...

    Just to make sure: You can't stop the macro with Ctrl+Pause?

    With Range.Find, it's easy to end up in an endless loop, since each Find sets the Range to the matched text.
    If you don't change the Range properly after the match, the same Range will be found again and again.

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

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

    Re: Range.Find bugginess (Word 2002 SP-2)

    To expand on Klaus' comment, using Range.Find incrementally (rather than for a Replace All operation) can be trickier than doing the same with the Selection object. As Klaus said, the Range you're searching may be constantly reset by the results of the search.

    You can adapt the following to handle the kind of search/replace you're trying to do. Just be sure you don't modify the .Wrap property. HTH!

    <pre>Sub FindReplaceWithRange()
    Dim rngToSearch As Range
    Dim rngResult As Range

    Set rngToSearch = ActiveDocument.Range
    Set rngResult = rngToSearch.Duplicate

    Do
    With rngResult.Find
    .ClearFormatting
    .Text = "Text to find"
    .Forward = True
    .Wrap = wdFindStop
    .Execute
    End With

    If rngResult.Find.Found = False Then
    Exit Do
    End If

    ''''''''''''''''''''''''''''''''''''
    ' Do stuff to found range, rngResult
    '
    ''''''''''''''''''''''''''''''''''''
    rngResult.MoveStart wdWord
    rngResult.End = rngToSearch.End
    Loop Until rngResult.Find.Found = False

    End Sub
    </pre>


  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: Range.Find bugginess (Word 2002 SP-2)

    Thanks to everyone for the responses. I believe there's definitely some Word bugginess involved here, and for the moment I'm retreating to using the Selection object in various places to avoid the problem. If anyone wants to look at the code that's triggering the freezes, I've attached it (along with some further explanation).

    Hans -- Testing the Range object after the .Execute line shows it's not Nothing, except when the freeze happens and the test line doesn't run.

    Jefferson -- I'd already found those threads. wdFindWrap is always set to wdFindStop when the freezes happen, and I collapse the Range to its end on the find loops.

    Klaus -- Ctrl-Break has no effect when these freezes happen. I generally right-click on the taskbar icon and select Close, whereupon Windows informs me that Word is not responding and I shut it down.

    Andrew -- Looking at your code caused me to try modifying mine in two ways. I tried setting the starting Range to ActiveDocument.Range (rather than ActiveDocument.Range(0, 0)), and I tried having the problematic functions work with a duplicate of the range rather than the Range they get passed. But neither of those adjustments changed the freeze behavior.
    Attached Files Attached Files

Posting Permissions

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