View Single Post
 
Old 04-06-2017, 04:20 AM
Thefirstfish` Thefirstfish` is offline Windows 10 Office 2016
Novice
 
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