none
Infinite Loop problem RRS feed

  • Question

  • Hi

    I have this code below where I am looking for a certain character style, replacing the character two spaces preceding it with a comma (when test in the character style is "abc") and inserting a period immediately following the character style regardless of the text.

    I have it working, but when I tried to do a loop it won't end. I've found that my method is probably not the correct one, but I can't figure out how to do it any other way. Is there a way to just tell the program to find an ending point that it will actually find?

    Code:

    Sub Macro9()
    '
    ' Macro9 Macro
    '
    '
        Do Until ActiveDocument.Bookmarks("\Sel") = _
        ActiveDocument.Bookmarks("\EndOfDoc")
        Selection.Find.ClearFormatting
        Selection.Find.Style = ActiveDocument.Styles("one_car")
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "abc"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.TypeText Text:=","
        Selection.Find.ClearFormatting
        Selection.Find.Style = ActiveDocument.Styles("one_car")
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeText Text:="."
        Loop
    End Sub

    Friday, October 11, 2013 1:42 PM

Answers

  • Hi,

    According to your code, the requirement is finding all of the specified character in document and looping insert characters. You can set Selection.Find.Wrap = wdFindStop, and use Selection.Find method return value as While condition statement.
    You can refer to the code below:

    'moves the selection to the beginning of the current document
    Selection.HomeKey Unit:=wdStory
    Dim IsEndofSearch As Boolean
    IsEndofSearch = True
    Do While IsEndofSearch
    Selection.Find.Replacement.ClearFormatting
         With Selection.Find
             .Text = "abc"
             .Replacement.Text = ""
             .Forward = True
             'The find operation ends if the beginning or end of the search range is reached.
             .Wrap = wdFindStop
             .Format = True
             .MatchCase = False
             .MatchWholeWord = False
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
         End With
         IsEndofSearch = Selection.Find.Execute
         Selection.MoveRight Unit:=wdCharacter, Count:=1
         Selection.TypeText Text:="."
    Loop
    

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 14, 2013 11:48 AM