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

Page 1 of 1 (4 items)
This post has 3 Replies | 0 Followers

Posts 2753
DominicM | Forum Activity | Posted: Sat, Sep 24 2011 7:08 AM

//---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

Posts 3578
steve clark | Forum Activity | Replied: Mon, Sep 26 2011 7:27 AM

Nice piece of code Dominic!

i have added it to Tools and Files supplied by users wiki page

QLinks, Bibl2, LLR, Macros
Dell Insp 17-5748, i5, 1.7 GHz, 8G RAM, win 8.1

Posts 612
John Brumett | Forum Activity | Replied: Mon, Sep 26 2011 6:39 PM

Maybe this is different but in Word 2010 in the Find and replace feature I can serarch for Font Size 12 Bold and Replace with Heading 1.  In the Replace Box I choose ^& (Find what text) in the Special Section and then under format I choose style and then Heading 1.  Most chapter heading are larger font then the rest of the text.  If that is not the case then you can search for certain terms and replace with Heading styles.

Posts 2753
DominicM | Forum Activity | Replied: Tue, Sep 27 2011 10:59 AM

useful to know John, I only have word 2007, so am lacking in features, but I did this because I mostly deal with plain text conversion so sadly no formatting,,

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

Page 1 of 1 (4 items) | RSS