#1
|
|||
|
|||
Workarounds for the find "dynamic" range bug in VBA?
I have a script in which I want to do a bunch of "find-and-replace"s on a range defined by a selection in Microsoft Word. Something like in the code snippet below. The problem is, after the first find & replace the selection range changes and the new second find & replace call operates on portions outside the user-defined selection made before running the script. (Discussion of this bug here.)
Are there any workarounds for this issue? For example, is there any way to store the selection range endpoint values, update them with changed values (since the effective range may have changed with any replacements made), and then use the changed range values as the new find & replace range? Code:
With Selection.Range.Find ' Replace "OLD" with "NEWTHING" .MatchCase = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = "OLD" .Replacement.Text = "NEWTHING" .Execute Replace:=wdReplaceAll End With With Selection.Range.Find ' Replace "REPLACEME" with "THEREPLACEMENT" .MatchCase = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = "REPLACEME" .Replacement.Text = "THEREPLACEMENT" .Execute Replace:=wdReplaceAll End With |
#2
|
||||
|
||||
I don't agree it's a bug. In your case at least it's a coding issue. Try:
Code:
With Selection.Range.Find .MatchCase = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = "OLD" .Replacement.Text = "NEWTHING" .Execute Replace:=wdReplaceAll .Text = "REPLACEME" .Replacement.Text = "THEREPLACEMENT" .Execute Replace:=wdReplaceAll End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I feel so dumb. But your suggestion saves the day. This forum makes me feel so ambivalent about myself, life, and the universe.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
"Dynamic" pie chars? | Markstar | Excel | 3 | 09-16-2015 10:02 AM |
I searched for "fatta" but Word can't find "Fatta" or "FATTA" why? | Jamal NUMAN | Word | 1 | 07-14-2011 02:05 AM |
How to choose a "List" for certain "Heading" from "Modify" tool? | Jamal NUMAN | Word | 2 | 07-03-2011 03:11 AM |
Outlook Web Access: How to load the "Microsoft Dynamic Html Editing Control" Add-On? | tomtom67 | Outlook | 1 | 08-31-2009 12:23 AM |
Mysterious "Subscript out of range" error | rnstewart | Excel | 4 | 12-29-2005 01:04 PM |