Styles Collection Object

Microsoft Word Visual Basic

DocumentStyles
Style
Multiple objects

A collection of Style objects that represent both the built-in and user-defined styles in a document.

Using the Styles Collection

Use the Styles property to return the Styles collection. The following example deletes all user-defined styles in the active document.

For Each sty In ActiveDocument.Styles
    If sty.BuiltIn = False Then sty.Delete
Next sty
		

Use the Add method to create a new user-defined style and add it to the Styles collection. The following example adds a new character style named "Introduction" and makes it 12-point Arial, with bold and italic formatting. The example then applies this new character style to the selection.

Set myStyle = ActiveDocument.Styles.Add(Name:="Introduction", _
    Type:=wdStyleTypeCharacter)
With myStyle.Font
    .Bold = True
    .Italic = True
    .Name = "Arial"
    .Size = 12
End With
Selection.Range.Style = "Introduction"
		

Use Styles(index), where index is the style name, a WdBuiltinStyle constant or index number, to return a single Style object. You must exactly match the spelling and spacing of the style name, but not necessarily its capitalization. The following example modifies the font of the user-defined style named "Color" in the active document.

ActiveDocument.Styles("Color").Font.Name = "Arial"
		

The following example sets the built-in Heading 1 style to not be bold.

ActiveDocument.Styles(wdStyleHeading1).Font.Bold = False
		

The style index number represents the position of the style in the alphabetically sorted list of style names. Note that Styles(1) is the first style in the alphabetic list. The following example displays the base style and style name of the first style in the Styles collection.

MsgBox "Base style= " _
    & ActiveDocument.Styles(1).BaseStyle & vbCr _
    & "Style name= " & ActiveDocument.Styles(1).NameLocal
		

Remarks

The Styles object isn't available from the Template object. However, you can use the OpenAsDocument method to open a template as a document so that you can modify styles in the template. The following example changes the formatting of the Heading 1 style in the template attached to the active document.

Set aDoc = ActiveDocument.AttachedTemplate.OpenAsDocument
With aDoc
    .Styles(wdStyleHeading1).Font.Name = "Arial"
    .Close SaveChanges:=wdSaveChanges
End With
		

Use the OrganizerCopy method to copy styles between documents and templates. Use the UpdateStyles method to update the styles in the active document to match the style definitions in the attached template.