Story Range Microsoft Word Add-in VBA (Visual Basic for Applications)

Story Range Microsoft Word Add-in VBA (Visual Basic for Applications)

StoryRanges

Refers to a collection of Ranges within document which has stories in it. In this article we will try to highlight the color of each story range in a document and also read Header/Footers story range using VBA.

Code example

Public Sub StoryRangeExample()
    'Declare document object
    Dim oDocument As Document
    'Bind active document
    Set oDocument = ActiveDocument
    
    'Declare story object
    Dim oStory As Object
    
    'Iterate each story and print number of sentences in it
    For Each oStory In oDocument.StoryRanges
        If oStory.StoryType <> wdMainTextStory Then
            If oStory.Font.Bold = True Then
                Debug.Print oStory.HighlightColorIndex = wdYellow
            End If
        End If
    Next oStory
    
    'Get header story
    oDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "This is my header text"
    Debug.Print oDocument.StoryRanges(wdPrimaryHeaderStory).Text
    
    'memory cleanup
    Set oStory = Nothing
    Set oDocument = Nothing
End Sub

Output

This is my header text

Next >> Fields in Microsoft Word Add-in with code example

Leave a Reply

Your email address will not be published.