1
votes

I shamelessly recorded a macro to amend the default heading styles 2 - 5 to change their .NextParagraphStyle to ones of my own making called Normal_lvl2, Normal_lvl3 etc :

With ActiveDocument.Styles("Heading 2").ParagraphFormat ' etc etc
    .LeftIndent = CentimetersToPoints(1.13)
    .RightIndent = CentimetersToPoints(0)
    .LineSpacingRule = wdLineSpaceDouble
    .Alignment = wdAlignParagraphLeft
    .FirstLineIndent = CentimetersToPoints(-0.63)
    .OutlineLevel = wdOutlineLevel2
    .NoSpaceBetweenParagraphsOfSameStyle = False
    .AutomaticallyUpdate = True
    .BaseStyle = "Normal"
    .NextParagraphStyle = "Normal_lvl2" ' here is the next style
End With

Problem is the document doesn't actually update the next paragraph style, either when I run the macro or set a style for a line manually. The new style works fine for the actual header line but the next paragraph is not changed.

I did try to loop through all paragraphs and set the style but it took far too long (I quit after 20 mins run time, the doc is 160 pages). Specifically I got all headings into an array, used Find to return a range for each of the headers in the array and set the next range style depending on the heading level. Maybe not the best way but I'm not too familiar with the Word Object Model.

So my question is - is there an efficient way to automate the application of my custom styles and to ensure the next paragraph style is also changed?

1
This seems like a misunderstanding. The NextParagraphStyle is not automatically applied to the next paragraph when you apply a style. It is used when you press return and a new paragraph gets inserted after the current paragraph. The new paragraph gets the NextParagraphStyleDirk Vollmar

1 Answers

2
votes

You should iterate over all paragraphs in your document and then adjust the following paragraph accordingly like it is done in the following sample:

Sub ChangeParagraphsAfterHeading()

    Dim para As Paragraph
    Dim nextPara As Paragraph

    For Each para In ActiveDocument.Paragraphs
        If para.Style = "Heading 2" Then
            Set nextPara = para.Next
            If Not nextPara Is Nothing Then
                nextPara.Style = "Normal_lvl2"
            End If
        End If
    Next

End Sub

I assume that you probably want to adjust the style for all paragraphs between two headings. The sample above doesn't do that yet, but it should get you started.