Consulting

Results 1 to 3 of 3

Thread: Do While - Loop - not working in loop

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location

    Do While - Loop - not working in loop

    Hi,
    I have a following code, and it is not working in a loop, because it is finding i convert searched text only one time not as many times as it occurrence.
    Thanks for helping.

    Sub FOTO()
                 
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
             
            Do While .Execute(FindText:="FOTO", MatchWholeWord:=True)
         
        oRng.End = oRng.Paragraphs(1).Range.End
         
                With oRng
                    .Font.Name = "Times New Roman"
                    .Font.Size = 5
                    '.Font.Italic = wdToggle
                    .bold = False
                    .Font.SmallCaps = False
                    .Font.AllCaps = True
                    With .ParagraphFormat
                        .LeftIndent = CentimetersToPoints(0)
                        .RightIndent = CentimetersToPoints(0)
                        .SpaceBefore = 0
                        .SpaceBeforeAuto = False
                        .SpaceAfter = 0
                        .SpaceAfterAuto = False
                        .LineSpacingRule = wdLineSpaceMultiple
                        .LineSpacing = LinesToPoints(0.9)
                        .Alignment = wdAlignParagraphJustify
                        .WidowControl = True
                        .KeepWithNext = False
                        .KeepTogether = False
                        .PageBreakBefore = False
                        .NoLineNumber = False
                        .Hyphenation = True
                        .FirstLineIndent = CentimetersToPoints(0)
                        .OutlineLevel = wdOutlineLevelBodyText
                        .CharacterUnitLeftIndent = 0
                        .CharacterUnitRightIndent = 0
                        .CharacterUnitFirstLineIndent = 0
                        .LineUnitBefore = 0
                        .LineUnitAfter = 0
                        .MirrorIndents = False
                        .TextboxTightWrap = wdTightNone
                    End With
                End With
                 
                Exit Do
            Loop
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub

  2. #2
    It only finds one instance because you have the line Exit Do which stops the loop after the first instance. You need to make a couple of changes for the loop to work throughout the document range.

                    End With
                    .Collapse 0 'add this line
                End With
    
                'Exit Do 'remove this line
            Loop
        End With
    lbl_Exit:
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    You solved it as usually.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •