Results 1 to 4 of 4
  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

    ReplaceAll in range (2002 SP-2)

    If you perform a range.Find.Execute(Replace:=wdReplaceAll), and if it so happens that the .Find.Text and the range.Text are exactly the same -- i.e., if the range consists of single instance of the Find target and nothing more -- Word ignores the end of the range and performs the replace throughout the rest of the document (starting with the range). If the range consists of the find target and a single additional character (e.g., a space), Word restricts the replace (as expected) to the range.

    I get the same unexpected behavior using the Replace dialog (if the Selection consists of a single instance of the Find target).

    Bug? Feature? Explanation, anyone?

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

    Re: ReplaceAll in range (2002 SP-2)

    I'd say it is by design. If you select a word in a document, then open the Replace dialog, the selected word is automatically placed in the 'Find what' box. You type something in the 'Replace with box'. It seems a reasonable assumption that if you then click 'Replace All', you intend to replace more than just the selected word, otherwise you could have clicked Replace. To display the prompt "Word has finished searching the selection. ..." in this situation would be irritating.

  3. #3
    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: ReplaceAll in range (2002 SP-2)

    I can definitely understand the rationale in the dialog-box scenario you describe, but I can't understand the carry-over of the same ignore-the-boundary approach where you're using ReplaceAll with a range object in VBA. And I note, as a follow-up, that the same ignore-the-range behavior happens where the find target involves wildcards.

    If you're not using wildcards, the workaround for the problem is a little kludgey but straightforward -- you test to see if rngX.Text is the target and, if it is, either set the range's .Text to the replacement or do ReplaceOne (instead of ReplaceAll) at that location. But if the Find target involves wildcards, what's the alternative test?

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

    Re: ReplaceAll in range (2002 SP-2)

    I suppose that the same decision mechanism is behind the interactive behavior and that in VBA. For wildcard searches it is inconvenient, I don't see an easy workaround.

Posting Permissions

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