Looking for a better way



Earlier I was helping a user that wanted a VBA find routine that would
start at the IP and go to the end of the document. On reaching the
end, the user wanted a pop-up to querry if the routine should loop to
the begining of the document. I got that far without any trouble.
However, I got tripped up trying to stop the routine at the IP after it
began searching from the document start. I scrapped together something
that appears workable, but it seems I am missing something simple.
Review and comments welcomed.

Sub ScratchMacro()
Dim oRng As Range
Dim oRng1 As Range
Dim oRng2 As Range
Dim i As Long
Dim bLooped As Boolean
Set oRng1 = ActiveDocument.Content
Set oRng2 = ActiveDocument.Content
i = Selection.Start
oRng1.Start = i
oRng2.End = i
Set oRng = oRng1
bLooped = False
LoopTwo:
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Font.Italic = True
.Wrap = wdFindStop
Do While .Execute
If bLooped = True And oRng.Start > i Then
MsgBox "Finished."
Exit Sub
End If
oRng.HighlightColorIndex = wdYellow
Select Case MsgBox("Do you want to remove italics from: " _
& oRng.Text, vbYesNoCancel, "Action")
Case Is = vbYes
oRng.Font.Italic = False
oRng.HighlightColorIndex = wdNoHighlight
oRng.Collapse wdCollapseEnd
Case Is = vbNo
oRng.HighlightColorIndex = wdNoHighlight
oRng.Collapse wdCollapseEnd
Case Is = vbCancel
oRng.HighlightColorIndex = wdNoHighlight
Exit Do
End Select
Loop
End With
If bLooped = False Then
If MsgBox("Do you want to loop to start?", _
vbYesNo, "Loop to Start") = vbYes Then
bLooped = True
Set oRng = oRng2
GoTo LoopTwo
End If
End If
End Sub

.



Relevant Pages

  • Re: Count Numerous Matches Only Once
    ... > then loop through the cells for a match. ... > The following routine returns Combin, expand as needed. ... > Dim r1 As Long ... > Private Function GetCombinationAs Long ...
    (microsoft.public.vb.general.discussion)
  • Re: Count Numerous Matches Only Once
    ... then loop through the cells for a match. ... The following routine returns Combin, expand as needed. ... Dim r1 As Long ... Private Function GetCombinationAs Long ...
    (microsoft.public.vb.general.discussion)
  • Run-time error 6
    ... As part of a routine to create worksheets the following macro looks at ... Dim X, C, xMax, y As Integer ... Dim S As String ... Loop ...
    (microsoft.public.excel)
  • Re: Excel dates seem to confuse me ...
    ... I have a VBA routine that calculates ISO week numbers. ... Dim ErrCount As Long ... 'loop until we get to the first week of 2000 ... > Function TILPeriodAs String ...
    (microsoft.public.excel.programming)
  • Re: Looping through Query to create multiple sheets in excel- Just need the loop
    ... I always get to this point and I can't figure out how to loop through ... I have put the string in the query at the bottom. ... Optional strWorkSheet As String, Optional strRange As ... Dim objXLSheet As Object 'Excel.Worksheet ...
    (microsoft.public.access.forms)