3
votes

I need a script which iterates through a word document and changes the style of paragraphs following a Headline style or an Image to a custom style without first line indent.

How do I loop through the paragraphs/headers/items in a word document? And how do I get the style? And how do I set the style afterwards?

The Goal is simple: I want the first line of a paragraph be indented, but not if the paragraph is following a Header line or image. And since this is a large document and I get those quite often I'd like some Kind of Automation and not try to do this by Hand.
So I'd like to write a script which is iterating through the paragraphs and changes the style from "paragraph" to "paragraph without indent" when it is after a header style or image.

1

1 Answers

3
votes

Here is some basic code to get you started here. Unfortunately, the Paragraph.Style parameter doesn't distinguish between text and images, but you can check and see if a Paragraph.Range object has any InlineShapes, which are images.

Sub indentParas()
    Dim doc As Document
    Set doc = ActiveDocument
    Dim para As Word.Paragraph
    Dim i As Boolean
    i = False
    For Each para In doc.Paragraphs

        If i = False Then
            para.IndentCharWidth 4
        End If

        If para.Range.InlineShapes.Count > 0 Then
            i = True
        ElseIf Left(para.Style, 7) = "Heading" Then
            i = True
        Else
            i = False
        End If
    Next
End Sub

Note: this is tested in Word 2010.