Word Macro: search for "Chapter" and apply style Heading 2 to paragraph

DominicM
DominicM Member Posts: 2,995 ✭✭✭
edited November 20 in English Forum

//---CODE---//

Sub FormatParagraphsContainingText()
Dim sTextToFind As String
Dim oStyleToUse As Style
Dim oRange As Range

sTextToFind = "Chapter"
Set oStyleToUse = ActiveDocument.Styles(wdStyleHeading2)
Set oRange = ActiveDocument.Range


With oRange

    'clear previous format criteria in the find dialog
    .Find.ClearFormatting
    .Find.Replacement.ClearFormatting


    With .Find
        .Text = sTextToFind
        .Forward = True
        .Wrap = wdFindStop
        .Format = False

        Do While .Execute()
            'Change the style 
            oRange.Paragraphs(1).Style = oStyleToUse
        Loop
    End With
End With
End Sub

//--END-CODE--//

the bold 2 can be changed to 3 etc if required for your various indent levels

 

Never Deprive Anyone of Hope.. It Might Be ALL They Have

Tagged:

Comments