TextStyles Collection

Microsoft Publisher Visual Basic

TextStyles Collection

Document TextStyles
TextStyle
Multiple objects

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

Using the TextStyles Collection

Use the TextStyles property to return the TextStyles collection. The following example creates a table and lists all the styles in the active publication.

Sub ListTextStyles()
    Dim sty As TextStyle
    Dim tbl As Table
    Dim intRow As Integer

    With ActiveDocument
        Set tbl = .Pages(1).Shapes.AddTable(NumRows:=.TextStyles.Count, _
            NumColumns:=2, Left:=72, Top:=72, Width:=488, Height:=12).Table
        For Each sty In .TextStyles
            intRow = intRow + 1
            With tbl.Rows(intRow)
                .Cells(1).text = sty.Name
                .Cells(2).text = sty.BaseStyle
            End With
        Next sty
    End With
End Sub
		

Use the Add method to create a new user-defined style and add it to the TextStyles collection. The following example creates a new style and applies it to the paragraph at the insertion point position.

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