Share via


TextStyle Object

Publisher Developer Reference

Represents a single built-in or user-defined style. The TextStyle object includes style attributes (font, font style, paragraph spacing, and so on) as properties of the TextStyle object. The TextStyle object is a member of the TextStyles collection. The TextStyles collection includes all the styles in the specified document.

Example

Use TextStyles(index), where index is the text style number or name, to return a single TextStyle object. You must exactly match the spelling and spacing of the style name, but not necessarily its capitalization.

The following example displays the style name and base style of the first style in the TextStyles collection.

Visual Basic for Applications
  Sub BaseStyleName()
    With ActiveDocument.TextStyles(1)
        MsgBox "Style name= " & .Name _
            & vbCr & "Base style= " & .BaseStyle
    End With
End Sub

Use the Add method to create a new style. To apply a style to a range, paragraph, or multiple paragraphs, set the TextStyle property to a user-defined or built-in style name. The following example creates a new style and applies it to the paragraph at the cursor position.

Visual Basic for Applications
  Sub ApplyTextStyle()
    Dim styNew As TextStyle
    Dim fntStyle As Font
'Create a new style
Set styNew = ActiveDocument.TextStyles.Add(StyleName:="NewStyle")
Set fntStyle = styNew.Font

'Format the Font object
With fntStyle
    .Name = "Tahoma"
    .Size = 20
    .Bold = msoTrue
End With

'Apply the Font object formatting to the new style
styNew.Font = fntStyle

'Apply the new style to the selected paragraph
Selection.TextRange.ParagraphFormat.TextStyle = "NewStyle"

End Sub

See Also