Compartilhar via


Style Object

Word Developer Reference

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

Remarks

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 name of the user-defined style named "Color" in the active document.

Visual Basic for Applications
  ActiveDocument.Styles("Color").Font.Name = "Arial"

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

Visual Basic for Applications
  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 alphabetical list. The following example displays the base style and style name of the first style in the Styles collection.

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

To apply a style to a range, paragraph, or multiple paragraphs, set the Style property to a user-defined or built-in style name. The following example applies the Normal style to the first four paragraphs in the active document.

Visual Basic for Applications
  Set myRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(1).Range.Start, _
    End:=ActiveDocument.Paragraphs(4).Range.End)
myRange.Style = wdStyleNormal

The following example applies the Heading 1 style to the first paragraph in the selection.

Visual Basic for Applications
  Selection.Paragraphs(1).Style = wdStyleHeading1

The following example creates a character style named "Bolded" and applies it to the selection.

Visual Basic for Applications
  Set myStyle = ActiveDocument.Styles.Add(Name:="Bolded", _
    Type:=wdStyleTypeCharacter)
myStyle.Font.Bold = True
Selection.Range.Style = "Bolded"

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. Use the OpenAsDocument method to open a template as a document so that you can modify the template styles.

See Also