Share via

Do While macro stuck in infinite loop

Anonymous
2016-07-25T22:58:54+00:00

Hi, 

I need to enclose italic strings in < and >. Using a found macro from 2002--too old, I guess--I wrote this:

With Selection.Find

 .ClearFormatting

 .Text = ""

 .Font.Italic = True

 .Replacement.Text = ""

 .Forward = True

 .Wrap = wdFindContinue

 .Format = True

 .MatchWildcards = False

End With

Selection.Find.Execute

Do While Selection.Find.Found

 Selection.InsertBefore "<"

 Selection.InsertAfter ">"

Selection.Find.Execute

Loop

End Sub

But this creates an infinite loop and crashes Word. Help?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2016-07-29T05:30:15+00:00

This should do it:

Sub doiteverywhere()

Dim myStoryRange As Range

'First search the main document using the Selection

Selection.HomeKey wdStory

Selection.Find.ClearFormatting

Selection.Find.Font.Italic = True

With Selection.Find

    Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True

        With Selection

            .InsertBefore "<"

            .InsertAfter ">"

            .Collapse wdCollapseEnd

        End With

    Loop

End With

'Now search all other stories using Ranges

For Each myStoryRange In ActiveDocument.StoryRanges

    If myStoryRange.StoryType <> wdMainTextStory Then

        myStoryRange.Select

        Selection.Collapse wdCollapseStart

        With Selection.Find

            Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True

                With Selection '.Range

                    .InsertBefore "<"

                    .InsertAfter ">"

                    .Collapse wdCollapseEnd

                End With

            Loop

        End With

        Do While Not (myStoryRange.NextStoryRange Is Nothing)

            Set myStoryRange = myStoryRange.NextStoryRange

            myStoryRange.Select

            Selection.Collapse wdCollapseStart

            With Selection.Find

                Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True

                    With Selection '.Range

                        .InsertBefore "<"

                        .InsertAfter ">"

                        .Collapse wdCollapseEnd

                    End With

                Loop

            End With

         Loop

     End If

Next myStoryRange

ActiveWindow.View = wdPrintView

Selection.HomeKey wdStory

End Sub

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-07-29T02:00:43+00:00

    Thanks, but I'm not at all sure how to merge your macro above with the template given on that web page. Here's my first shot at it, which I'm sure is wrong:

    Dim myStoryRange As Range

    For Each myStoryRange In ActiveDocument.StoryRanges

    myStoryRange.Find.ClearFormatting

    myStoryRange.Find.Font.Italic = True

    With myStoryRange.Find

        Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True

            With Selection

                .InsertBefore "<"

                .InsertAfter ">"

                .Collapse wdCollapseEnd

            End With

        Loop

    End With

    Any clues?

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-07-26T07:41:19+00:00

    See the article "Using a macro to replace text where ever it appears in a document including Headers, Footers, Textboxes, etc.” at:

    http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-07-26T00:53:32+00:00

    Thanks so much! Works beautifully. One question, though: would there be a way to loop this through all story ranges? Or am I pushing my luck?

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-07-25T23:52:34+00:00

    Use:

    Selection.HomeKey wdStory

    Selection.Find.ClearFormatting

    Selection.Find.Font.Italic = True

    With Selection.Find

        Do While .Execute(FindText:="", MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True

            With Selection

                .InsertBefore "<"

                .InsertAfter ">"

                .Collapse wdCollapseEnd

            End With

        Loop

    End With

    Was this answer helpful?

    0 comments No comments