Find and replace
-
Hello, This is partly an MS-Word question but I am using VB subroutines which is where my problem arises. I have created a find and replace script that uses wildcards to find a pattern throughout a Microsoft Word 2003 document and replace the pattern with itself but with a different font color. The find and replace portions both work great as I am recording the macro (finding all instances of the pattern and changing the color). However, when I go back and run the macro to test it, it only finds the first instance of my pattern. Word apparently does not have the ability to record finds for all instances of a pattern in one pass. I tried putting a EOF or LOF function to force the macro to run until the end of file is reached but I get caught in an endless loop and I have to shut down the application all together. Does anyone know of a way of performing this action. Below is my code without a loop where only the first instance of the pattern is found. Thanks
Sub test()
'
' test Macro
' Macro recorded 8/5/2006 by Harold Woodall
'
Selection.Find.ClearFormatting
For Counter = 1 To 500
With Selection.Find
.Text = "(\>[A-Z,a-z]{1,250}\<)"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = True
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorBlue
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
End With
Next
End Sub