Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-06-2017, 04:20 AM
Thefirstfish` Thefirstfish` is offline Infinite loops occurring in find and replace functions in word macro Windows 10 Infinite loops occurring in find and replace functions in word macro Office 2016
Novice
Infinite loops occurring in find and replace functions in word macro
 
Join Date: Dec 2015
Posts: 11
Thefirstfish` is on a distinguished road
Default Infinite loops occurring in find and replace functions in word macro

Dear Word VBA Forum,



I am having a recurring problem with infinite loops in a macro containing multiple find and replace functions. Each function is written as follows:

Code:
Set oRng = ActiveDocument.Range
    With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Trizol"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWholeWord = True
    .MatchCase = False
    .Execute
  End With
  Do While oRng.Find.Found = True
    oRng.Text = "TRIzolŪ"
    oRng.Collapse Direction:=wdCollapseEnd
    oRng.Find.Execute
  Loop

The loops happen if even one instance of the target text is found - but only in about 50% of documents, while in the other documents the code works perfectly.

I'm not sure what the difference between the 'problem' and 'non-problem' documents is. I tried removing all fields and turning track changes off in one of the problem documents, which did not prevent the infinite loops.

Any help would be gratefully appreciated!

Thank you

Last edited by macropod; 04-06-2017 at 04:21 AM. Reason: Added code tags
Reply With Quote
  #2  
Old 04-06-2017, 04:28 AM
macropod's Avatar
macropod macropod is offline Infinite loops occurring in find and replace functions in word macro Windows 7 64bit Infinite loops occurring in find and replace functions in word macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There doesn't appear to be anything 'wrong' with your code, but I can't see any point to the loop, either - you could do the same with:
Code:
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "Trizol"
  .Replacement.Text = "TRIzolŪ"
  .Forward = True
  .Format = False
  .MatchCase = False
  .Wrap = wdFindContinue
  .MatchWholeWord = True
  .Execute Replace:=wdReplaceAll
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-06-2017, 10:06 AM
Thefirstfish` Thefirstfish` is offline Infinite loops occurring in find and replace functions in word macro Windows 10 Infinite loops occurring in find and replace functions in word macro Office 2016
Novice
Infinite loops occurring in find and replace functions in word macro
 
Join Date: Dec 2015
Posts: 11
Thefirstfish` is on a distinguished road
Default

Thanks Macropod! Your code fixed the infinite loop issue for that function and similar ones. The macro seems to run faster too.

However, I'm not sure how to convert other infinite-loop-prone functions that only replace parts of strings to your suggested format. For example:

Code:
 With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[0-9]%-[0-9]"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .MatchWholeWord = False
    .Execute
  End With
  Do While oRng.Find.Found = True
    oRng.Characters(2).Text = ""
    oRng.Collapse Direction:=wdCollapseEnd
    oRng.Find.Execute
  Loop
Is there a simple way to do that? Many thanks.

Last edited by macropod; 04-06-2017 at 03:04 PM. Reason: Replaced quote tags around code with code tags
Reply With Quote
  #4  
Old 04-06-2017, 03:13 PM
macropod's Avatar
macropod macropod is offline Infinite loops occurring in find and replace functions in word macro Windows 7 64bit Infinite loops occurring in find and replace functions in word macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That's as simple as:
Code:
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "([0-9])%(-[0-9])"
  .Replacement.Text = "\1\2"
  .Forward = True
  .Format = False
  .Wrap = wdFindContinue
  .MatchWildCards = True
  .Execute Replace:=wdReplaceAll
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-06-2017, 05:33 PM
Thefirstfish` Thefirstfish` is offline Infinite loops occurring in find and replace functions in word macro Windows 10 Infinite loops occurring in find and replace functions in word macro Office 2016
Novice
Infinite loops occurring in find and replace functions in word macro
 
Join Date: Dec 2015
Posts: 11
Thefirstfish` is on a distinguished road
Default

Thanks so much. The code you posted and some minor tweaks have solved most of the issues I've been having.

For some reason, having tracked changes on makes the above code behave oddly in some situations (e.g., one function changes "1+1" to "1 + 1" with tracked changes off, but with tracked changes on the output is "11+ ").

However, I can work around that by running the macro with tracked changes off then comparing documents to add the tracking.

The only thing I still can't automate is to remove the percentage sign in text such as "20%-40% when an en dash is used instead of a hyphen. However, I have about 150 functions in the macro so having only one misbehaving isn't bad.
Reply With Quote
  #6  
Old 04-06-2017, 07:18 PM
macropod's Avatar
macropod macropod is offline Infinite loops occurring in find and replace functions in word macro Windows 7 64bit Infinite loops occurring in find and replace functions in word macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Thefirstfish` View Post
For some reason, having tracked changes on makes the above code behave oddly in some situations (e.g., one function changes "1+1" to "1 + 1" with tracked changes off, but with tracked changes on the output is "11+ ").
Yes, that's a known 'Track Changes' behaviour. You could have the macro toggle & restore the 'Track Changes' state.
Quote:
Originally Posted by Thefirstfish` View Post
The only thing I still can't automate is to remove the percentage sign in text such as "20%-40% when an en dash is used instead of a hyphen. However, I have about 150 functions in the macro so having only one misbehaving isn't bad.
That's easily resolved by changing
.Text = "([0-9])%(-[0-9])"
to:
.Text = "([0-9])%(?[0-9])"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Infinite loops occurring in find and replace functions in word macro Need Macro for Find and Replace, Inserting logo in Ms-Word. Aswinraj Word VBA 1 06-05-2016 04:33 PM
Find and Replace Macro Need djpemberton Word VBA 2 05-04-2016 07:53 PM
Infinite loops occurring in find and replace functions in word macro Find and Replace Macro amparete13 PowerPoint 3 03-11-2014 05:29 AM
Infinite loops occurring in find and replace functions in word macro Word VBA Macro to Find and Replace based on the Alt Text of an Image bennymc Word VBA 1 01-27-2014 04:23 PM
Find and Replace Macro - A Better Way Tribos Word VBA 0 10-08-2008 03:22 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:57 AM.


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