Article ID: 170725
Article Last Modified on 1/20/2007
Sub EditFindLoopExample()
'This example inserts "Tip: " at the beginning of
' every paragraph formatted with the Heading 3 style.
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleHeading3
'The Do...Loop statement repeats a series of
' actions each time this style is found.
Do While .Execute(Forward:=True, Format:=True) = True
With .Parent
'If the found text is the last
' paragraph in the document...
If .End = ActiveDocument.Content.End Then
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "Tip: "
Exit Do
'If the found text is *not* the last
' paragraph in the document...
Else
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "Tip: "
.Move Unit:=wdParagraph, Count:=1
End If
End With
'Goes back to the beginning of the Do...Loop statement.
Loop
End With
End Sub
For more information about searching text in Word documents, from the
Visual Basic Editor, click the Office Assistant, type "find," click Search,
and then click to view "Find Object."
176476 OFF: Office Assistant Not Answering Visual Basic Questions
173707 OFF97: How to Run Sample Code from Knowledge Base Articles
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: vb vba vbe
Keywords: kbdtacode kbhowto kbmacroexample KB170725