Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-07-2016, 09:30 AM
Robert K S Robert K S is offline Workarounds for the find "dynamic" range bug in VBA? Windows 7 64bit Workarounds for the find "dynamic" range bug in VBA? Office 2007
Novice
Workarounds for the find "dynamic" range bug in VBA?
 
Join Date: Jul 2016
Location: Cleveland, Ohio
Posts: 10
Robert K S is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 11-07-2016, 12:51 PM
macropod's Avatar
macropod macropod is offline Workarounds for the find "dynamic" range bug in VBA? Windows 7 64bit Workarounds for the find "dynamic" range bug in VBA? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,182
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

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]
Reply With Quote
  #3  
Old 11-07-2016, 01:00 PM
Robert K S Robert K S is offline Workarounds for the find "dynamic" range bug in VBA? Windows 7 64bit Workarounds for the find "dynamic" range bug in VBA? Office 2007
Novice
Workarounds for the find "dynamic" range bug in VBA?
 
Join Date: Jul 2016
Location: Cleveland, Ohio
Posts: 10
Robert K S is on a distinguished road
Default

I feel so dumb. But your suggestion saves the day. This forum makes me feel so ambivalent about myself, life, and the universe.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Workarounds for the find "dynamic" range bug in VBA? "Dynamic" pie chars? Markstar Excel 3 09-16-2015 10:02 AM
Workarounds for the find "dynamic" range bug in VBA? I searched for "fatta" but Word can't find "Fatta" or "FATTA" why? Jamal NUMAN Word 1 07-14-2011 02:05 AM
Workarounds for the find "dynamic" range bug in VBA? How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM
Workarounds for the find "dynamic" range bug in VBA? Outlook Web Access: How to load the "Microsoft Dynamic Html Editing Control" Add-On? tomtom67 Outlook 1 08-31-2009 12:23 AM
Workarounds for the find "dynamic" range bug in VBA? Mysterious "Subscript out of range" error rnstewart Excel 4 12-29-2005 01:04 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 10:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft