Microsoft KB Archive/267984

= How To Create and Use Custom Styles with Automation =

Article ID: 267984

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition, when used with:
 * Microsoft Word 95 Standard Edition

-



This article was previously published under Q267984



SUMMARY
Styles in Word make it easy to apply the same formatting to different parts of a document. A style contains information about the font, paragraph formatting, borders, shading, tabs, and more. There are a number of styles built into Word, but it's also possible to define custom styles.

Every style is based on another style (the &quot;base style&quot;) and inherits its characteristics from that style. To customize the new style, change its properties.

Once you create a custom style, any changes to the base style are reflected in it.



MORE INFORMATION
To create a new style:   Create an instance of the Word application server: oWord = CreateObject(&quot;Word.Application&quot;)   Create a new document: oDocument = oWord.Documents.Add Alternatively, you can open an existing document using the Open method, passing the full path to the document: oDocument = oWord.Documents.Open   Use the Add method of the Document's Styles collection, passing the name for the new style and a constant that indicates whether the new style is a paragraph style (wdStyleTypeParagraph = 1) or a character style (wdStyleTypeParagraph = 2): oStyle = oDocument.Styles.Add   Set the base style for the new style to an existing style: oStyle.BaseStyle = oDocument.Styles[ |  ]  Set properties of the new style that are different than those of the base style.  To apply the new style to a range in the document, refer to it by the name assigned when it was created or by using an existing object reference: oRange.Style = oDocument.Styles[ ] -or- oRange.Style = oStyle </li> When the document is saved, the new style is saved with it.</li></ol>

This example creates a new paragraph style based on the built-in Normal style. The new style is identical to the Normal style, except that it is italic and centered. Then, the new style is used.
 * 1) DEFINE wdStyleTypeParagraph 1
 * 2) DEFINE wdStyleNormal -1
 * 3) DEFINE wdAlignParagraphCenter 1
 * 4) DEFINE wdCollapseEnd 0

LOCAL oWord, oDocument, oItalicStyle

oWord = CreateObject(&quot;Word.Application&quot;) oWord.Visible = .T.
 * Instantiate Word and create a document

oDocument = oWord.Documents.Add

oItalicStyle = ; oDocument.Styles.Add(&quot;CenteredItalic&quot;, wdStyleTypeParagraph)
 * Create a new style

WITH oItalicStyle .BaseStyle = oDocument.Styles[ wdStyleNormal ] .Font.Italic = .T.  .ParagraphFormat.Alignment = wdAlignParagraphCenter ENDWITH
 * Set style properties

oRange = oDocument.Range(0, 0) oRange.Style = oItalicStyle oRange.InsertAfter(&quot;This uses the CenteredItalic style&quot;) oRange.InsertParagraphAfter oRange.Collapse( wdCollapseEnd )
 * Add some text in the new style

oRange.End = oRange.End + 1 && so Style change won't affect previous text oRange.Style = oDocument.Styles[ wdStyleNormal ] oRange.InsertAfter(&quot;This uses the Normal style&quot;) oRange.InsertParagraphAfter
 * Now add some text in the Normal style

RETURN

Keywords: kbhowto kbautomation KB267984

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.