StoryRanges Collection Object

Microsoft Word Visual Basic

Show All

StoryRanges Collection Object

         
Documents (Document) StoryRanges (Range)
Multiple objects

A collection of Range objects that represent stories in a document.

Using the StoryRanges Collection

Use the StoryRanges property to return the StoryRanges collection. The following example removes manual character formatting from the text in all stories other than the main text story in the active document.

For Each aStory In ActiveDocument.StoryRanges
    If aStory.StoryType <> wdMainTextStory Then aStory.Font.Reset
Next aStory

The Add method isn't available for the StoryRanges collection. The number of stories in the StoryRanges collection is finite.

Use StoryRanges(index), where index is a WdStoryType constant, to return a single story as a Range object. The following example adds text to the primary header story and then displays the text.

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range _
    .Text = "Header text"
MsgBox ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Text

The following example copies the text of the footnotes from the active document into a new document.

If ActiveDocument.Footnotes.Count >= 1 Then
    ActiveDocument.StoryRanges(wdFootnotesStory).Copy
    Documents.Add.Content.Paste
End If

Remarks

If you attempt to return a story that isn't available in the specified document, an error occurs. The following example determines whether or not a footnote story is available in the active document.

On Error GoTo errhandler
Set MyRange = ActiveDocument.StoryRanges(wdFootnotesStory)
errhandler:
If Err = 5941 Then MsgBox "The footnotes story is not available."

Use the NextStoryRange property to loop through all stories in a document. The following example searches each story in the active document for the text "Microsoft Word." When the text is found, it's formatted as italic.

For Each myStoryRange In ActiveDocument.StoryRanges
    myStoryRange.Find.Execute _
        FindText:="Microsoft Word", Forward:=True
    While myStoryRange.Find.Found
        myStoryRange.Italic = True
        myStoryRange.Find.Execute _
            FindText:="Microsoft Word", Forward:=True
    Wend
    While Not (myStoryRange.NextStoryRange Is Nothing)
        Set myStoryRange = myStoryRange.NextStoryRange
        myStoryRange.Find.Execute _
            FindText:="Microsoft Word", Forward:=True
        While myStoryRange.Find.Found
            myStoryRange.Italic = True
            myStoryRange.Find.Execute _
                FindText:="Microsoft Word", Forward:=True
        Wend
    Wend
Next myStoryRange