Resetting font search/replace goes in a loop

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Resetting font search/replace goes in a loop

Post by Robie »

Hi

I am running a macro to reset the 'Courier New' for any words found in the document. This works fine if the 'Courier New' word is *not* in the table.

If the 'Courier New' word is in the table, it goes in a loop.

The code is as follows:

Code: Select all

Function ResetCourierNewWords()
    ' ------------------------------------------------------------------------------
    ' Do not courierise blank paragraphs, etc.
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Font.Name = "Courier New"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    While (Selection.Find.Execute)
        Selection.Font.Reset
        Selection.Collapse Direction:=wdCollapseEnd
    Wend
End Function
The text is pretty simple with only one 'Courier New' word in the 1st cell of the table. I have attached the document and this image is as follows:
51.png
I am sure it is something stupid I am missing.
Thanks.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Resetting font search/replace goes in a loop

Post by HansV »

Simply remove the line to collapse the selection. Here is a slightly streamlined version:

Code: Select all

Function ResetCourierNewWords()
    ' ------------------------------------------------------------------------------
    ' Do not courierise blank paragraphs, etc.
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Font.Name = "Courier New"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While .Execute
            Selection.Font.Reset
        Loop
    End With
End Function
Best wishes,
Hans