Etudes for Microsoft Word Programmers. Appendix.


Information on this page is from Microsoft MSDN http://msdn.microsoft.com.

Style (Word 2003)

Style Object

Represents a single built-in or user-defined style. The Style object includes style attributes (font, font style, paragraph spacing, and so on) 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.

Using the Style Object

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.

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

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.

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.

Selection.Paragraphs(1).Style = wdStyleHeading1

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

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.

Style Object Properties

Style object properties are described in the following table.

Property

Type

Description

AutomaticallyUpdate

Read/write Boolean

True if the style is automatically redefined based on the selection. False if Word prompts for confirmation before redefining the style based on the selection. A style can be redefined when it's applied to a selection that has the same style but different manual formatting.

BaseStyle

Read/write Variant

Returns or sets an existing style on which you can base the formatting of another style. To set this property, specify either the local name of the base style, an integer or a wdBuiltinStyle constant, or an object that represents the base style.

Borders

Borders Collection

Returns a Borders collection that represents all the borders for the specified object.

BuiltIn

Read-only Boolean

True if the specified object is one of the built-in styles or caption labels in Word.

You can specify built-in styles across all languages by using the WdBuiltinStyle constants or within a language by using the style name for the language version of Word.

Description

Read-only
String

Returns the description of the specified style. For example, a typical description for the Heading 2 style might be "Normal + Font: Arial, 12 pt, Bold, Italic, Space Before 12 pt After 3 pt, KeepWithNext, Level 2."

Font

Read/write
Font

Returns or sets a Font object that represents the character formatting of the specified object. To set this property, specify an expression that returns a Font object.

Frame

Read-only
Frame Object

Returns a Frame object that represents the frame formatting for the specified style or find-and-replace operation.

InUse

Read-only Boolean

True if the specified style is a built-in style that has been modified or applied in the document or a new style that has been created in the document.

This property doesn't necessarily indicate whether the style is currently applied to any text in the document. For instance, if text that's been formatted with a style is deleted, the InUse property of the style remains True. For built-in styles that have never been used in the document, this property returns False.

LanguageID

Read/write
WdLanguageID

Returns or sets the language for the specified object.

LinkStyle

Read/write
Variant

Sets or returns a Variant that represents a link between a paragraph and a character style.

When a character style and a paragraph style are linked, the two styles take on the same character formatting.

ListLevelNumber

Read-only
Long

Returns the list level for the specified style.

ListTemplate

ListTemplate Object

Returns a ListTemplate object that represents the list formatting for the specified Style or ListFormat object.

A list template includes all the formatting that defines a particular list. Each of the seven formats (excluding None) found on each of the tabs in the Bullets and Numbering dialog box (Format menu) corresponds to a list template. Documents and templates can also contain collections of list templates.

If the first paragraph in the range for the ListFormat object is not formatted as a list, the ListTemplate property returns Nothing.

NameLocal

Read/write
String

Returns the name of a built-in style in the language of the user. Setting this property renames a user-defined style or adds an alias to a built-in style.

NextParagraphStyle

Read/write Variant

Returns or sets the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style. To set this property, specify either the local name of the next style, an integer or a WdBuiltinStyle constant, or an object that represents the next style.

NoProofing

Read/write
Long

True if the spelling and grammar checker ignores text formatted with this style.

NoSpaceBetween
ParagraphsOfSameStyle

Read/write Boolean

True for Microsoft Word to remove spacing between paragraphs that are formatted using the same style.

ParagraphFormat

Read/write
ParagraphFormat

Returns or sets a ParagraphFormat object that represents the paragraph settings for the specified range, selection, find or replacement operation, or style.

Shading

Shading Object

Returns a Shading object that refers to the shading formatting for the specified object.

Table

TableStyle Object

Returns a TableStyle object representing properties that can be applied to a table using a table style.

Type

Read-only WdStyleType

Returns the style type.

 

При установке свойства Style можно использовать строку задающую имя стиля, положительное целое число, константу из перечисления wdBuiltinStyle или отрицательное целое число, а также объект типа Style.

Enumeration WdStyleType

Enumeration WdStyleType defines style type.

Constant Value
wdStyleTypeParagraph 1
wdStyleTypeCharacter 2
wdStyleTypeTable 3
wdStyleTypeList 4

Styles Collection

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.

Enumeration WdBuiltinStyle

Enumeration WdBuiltinStyle enumerates built-in styles.

Constant Value Constant Value Constant Value
wdStyleBlockQuotation -85 wdStyleHtmlCite -98 wdStyleListContinue4 -72
wdStyleBodyText -67 wdStyleHtmlCode -99 wdStyleListContinue5 -73
wdStyleBodyText2 -81 wdStyleHtmlDfn -100 wdStyleListNumber -50
wdStyleBodyText3 -82 wdStyleHtmlKbd -101 wdStyleListNumber2 -59
wdStyleBodyTextFirstIndent -78 wdStyleHtmlNormal -95 wdStyleListNumber3 -60
wdStyleBodyTextFirstIndent2 -79 wdStyleHtmlPre -102 wdStyleListNumber4 -61
wdStyleBodyTextIndent -68 wdStyleHtmlSamp -103 wdStyleListNumber5 -62
wdStyleBodyTextIndent2 -83 wdStyleHtmlTt -104 wdStyleMacroText -46
wdStyleBodyTextIndent3 -84 wdStyleHtmlVar -105 wdStyleMessageHeader -74
wdStyleCaption -35 wdStyleHyperlink -86 wdStyleNavPane -90
wdStyleClosing -64 wdStyleHyperlinkFollowed -87 wdStyleNormal -1
wdStyleCommentReference -40 wdStyleIndex1 -11 wdStyleNormalIndent -29
wdStyleCommentText -31 wdStyleIndex2 -12 wdStyleNormalTable -106
wdStyleDate -77 wdStyleIndex3 -13 wdStyleNoteHeading -80
wdStyleDefaultParagraphFont -66 wdStyleIndex4 -14 wdStylePageNumber -42
wdStyleEmphasis -89 wdStyleIndex5 -15 wdStylePlainText -91
wdStyleEndnoteReference -43 wdStyleIndex6 -16 wdStyleSalutation -76
wdStyleEndnoteText -44 wdStyleIndex7 -17 wdStyleSignature -65
wdStyleEnvelopeAddress -37 wdStyleIndex8 -18 wdStyleStrong -88
wdStyleEnvelopeReturn -38 wdStyleIndex9 -19 wdStyleSubtitle -75
wdStyleFooter -33 wdStyleIndexHeading -34 wdStyleTableOfAuthorities -45
wdStyleFootnoteReference -39 wdStyleLineNumber -41 wdStyleTableOfFigures -36
wdStyleFootnoteText -30 wdStyleList -48 wdStyleTitle -63
wdStyleHeader -32 wdStyleList2 -51 wdStyleTOAHeading -47
wdStyleHeading1 -2 wdStyleList3 -52 wdStyleTOC1 -20
wdStyleHeading2 -3 wdStyleList4 -53 wdStyleTOC2 -21
wdStyleHeading3 -4 wdStyleList5 -54 wdStyleTOC3 -22
wdStyleHeading4 -5 wdStyleListBullet -49 wdStyleTOC4 -23
wdStyleHeading5 -6 wdStyleListBullet2 -55 wdStyleTOC5 -24
wdStyleHeading6 -7 wdStyleListBullet3 -56 wdStyleTOC6 -25
wdStyleHeading7 -8 wdStyleListBullet4 -57 wdStyleTOC7 -26
wdStyleHeading8 -9 wdStyleListBullet5 -58 wdStyleTOC8 -27
wdStyleHeading9 -10 wdStyleListContinue -69 wdStyleTOC9 -28
wdStyleHtmlAcronym -96 wdStyleListContinue2 -70    
wdStyleHtmlAddress -97 wdStyleListContinue3 -71    

WdBuiltinStyle values are in range from -1 (wdStyleNormal) to -106 (wdStyleNormalTable).

Built-in styles with values -92, -93 and -94 exist (f.e. -92 corresponds to "E-mail Signature" style), but these constants are not described neither in Microsoft Word help system nor in the MSDN - see the page "Microsoft Word Enumerated Constants" on the MSDN Web site.

The following code will print style "E-mail Signature" (-92), but will not print styles "z-Top of Form" (-93) and  "z-Bottom of Form" (-94).

Sub AhPrintStylesFromIndex()
    For Index = 1 To ActiveDocument.Styles.Count
        Debug.Print ActiveDocument.Styles(Index).NameLocal
    Next Index
End Sub

 

Etudes for Microsoft Word Programmers. Appendix. Object Style.


Unless otherwise noted, all materials on this site are
© 2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site.
www.transcriber.ru | E-mail the Author