4.5.4 Working with styles

Working with styles

While it s appropriate to manually adjust the formatting of a word, sentence, or paragraph here and there, the most effective way to use Word is to take advantage of styles, which are named formats that you can apply to a portion of a document. When you re working in Word, you can see the style for the insertion point in the first dropdown on the Formatting toolbar.

Word has two kinds of styles: paragraph styles and character styles. Character styles are used for fragments and control only a few settings, primarily font-related. Paragraph styles, as the name implies, apply to entire paragraphs and include a lot more options. Paragraph styles can specify font and paragraph formatting, as well as tab settings and much more. In the Style dialog available from Word s Format menu (see Figure 6), paragraph styles are preceded by a paragraph marker, while character styles begin with an underlined "a."

Using styles is much like using classes in an object-oriented language. They make it easy to enforce uniformity throughout and across documents, and they let you change the characteristics of sections of text with a single change. Word s styles offer some other benefits, as well. For example, each paragraph style sets the default style for the paragraph to follow. With a little more work up front, styles can be used to provide an outline for a document, as well.

Figure 6. Creating and choosing styles. Word s Style dialog lists the available styles. Paragraph styles are preceded by a paragraph symbol, while an underlined "a" precedes character styles. Note in the Description that the style is described in terms of another its "base style."

The Document object includes a Styles collection, which contains one Style object for each of the styles stored in the document. You can add your own styles using the Styles collection s Add method. Various objects Style properties point to Style objects.

What all this fuss about styles means is that, rather than writing a lot of code to change fonts and sizes, and to set alignment and leading and other things like that, you can simply define a few custom styles or modify built-in styles, and then apply them to your documents as needed. For example, this code modifies the Normal style, which is always available, to use 16-point centered Garamond italic:

#DEFINE wdStyleNormal -1

#DEFINE wdAlignParagraphCenter 1

WITH oWord.ActiveDocument.Styles[ wdStyleNormal ]

WITH .Font

.Name = "Garamond"

.Size = 16

.Italic = .T.

ENDWITH

.ParagraphFormat.Alignment = wdAlignParagraphCenter

ENDWITH

To apply an existing style to a portion of a document, set the Style property of the Range or Paragraph to a built-in style using a constant, or to the name of a custom style. Table 5 lists the constants for some of the more commonly used built-in styles. This example applies the Heading 1 style to the range referenced by oRange:

#DEFINE wdStyleHeading1 -2

oRange.Style = oWord.ActiveDocument.Styles[ wdStyleHeading1 ]

Table 5. Built-in styles. Word has more than 100 built-in styles, each referenced by a defined constant. This table shows just a few of the most common; use the Object Browser to find the rest.

Constant

Value

Constant

Value

wdStyleNormal

-1

wdStyleHeading1

-2

wdStyleBodyText

-67

wdStyleHeading2

-3

wdStyleDefaultParagraphFont

-66

wdStyleHeading3

-4

Creating custom styles

In addition to modifying the built-in styles, you can create your own custom styles. To add a new style, use the Add method of the Styles collection. Add takes two parameters: the name of the new style, and the Word constant that indicates whether it s a paragraph style (wdStyleTypeParagraph = 1) or a character style (wdStyleTypeCharacter = 2).

Every style is based on an existing style. By default, new paragraph styles are based on the Normal style, and new character styles are based on the Default Character Font style. The BaseStyle property indicates which style another style inherits from, however.

Whatever style BaseStyle points to, all other changes to the style s properties use the BaseStyle as their point of reference. If you look at the Style dialog, you see that the style s characteristics are described as "<The base style>" + "<various other characteristics>." The Description property contains the same information. So, much like classes in OOP, changes to the base style change any styles based on it.

Table 6 lists key properties of the Style object, along with significant constant values.

Table 6. Style counts. Styles are Word s version of OOP. They offer a way to provide uniform formatting within and across documents.

Property

Type

Description

BaseStyle

Character, Numeric, or Object

The name, constant value, or pointer to the style on which this style is based. See Table 5 and Help or the Object Browser for constant values for built-in styles.

Type

Numeric

The kind of style paragraph or character.

wdStyleTypeParagraph

1

wdStyleTypeCharacter

2

 

Builtin

Logical

Indicates whether this a built-in style.

Description

Character

The description of the style (as shown in the Style dialog).

Font

Object

Pointer to a Font object for the style.

ParagraphFormat

Object

Pointer to a ParagraphFormat object for the style.

Borders

Object

Pointer to a Borders collection for the style.

Shading

Object

Pointer to a Shading object for the style.

NextParagraphStyle

Character, Numeric, or Object

The name, constant value, or pointer to the style for the paragraph to follow this paragraph, for paragraph styles.

Listing 2 takes the simple customer address document from earlier in the chapter and begins to create a document worthy of Word. It creates several new styles to do the job. In practice, you could use the built-in Normal and Heading X (there are multiple heading levels) styles for this document, redefining them as needed. But the example shows how easy it is to create new styles. (You ll find this program as Styles.PRG in the Developer Download files available at www.hentzenwerke.com.)

Listing 2. This program creates several custom styles, then uses them to create the document in Figure 7.

* Create a formatted document by sending data from one record.

* Demonstrates Style objects, but it's more likely the needs here

* could be met by existing styles.

#DEFINE CR CHR(13)

#DEFINE wdStyleTypeParagraph 1

#DEFINE wdStyleNormal -1

#DEFINE wdAlignParagraphLeft 0

#DEFINE wdAlignParagraphCenter 1

#DEFINE wdCollapseEnd 0

USE _SAMPLES + "TasTrade\Data\Customer"

RELEASE ALL LIKE o*

PUBLIC oWord

LOCAL oWord, oDocument, oRange

LOCAL oBodyStyle, oMajorHeadingStyle, oMinorHeadingStyle

oWord = CreateObject("Word.Application")

oWord.Visible = .T.

oDocument = oWord.Documents.Add() && Use the Normal template

oRange = oDocument.Range()

* Set up styles. Base body style on Normal.

oBodyStyle = oDocument.Styles.Add( "Body", wdStyleTypeParagraph )

WITH oBodyStyle

* This line is overkill since it's the default

.BaseStyle = oDocument.Styles[ wdStyleNormal ]

WITH .Font

.Name = "Arial"

.Size = 12

ENDWITH

WITH .ParagraphFormat

* These are fairly normal defaults, so these lines

* may not be necessary

.Alignment = wdAlignParagraphLeft

.SpaceAfter = 0

ENDWITH

ENDWITH

* Major heading is big and centered.

oMajorHeadingStyle = oDocument.Styles.Add( "MajorHeading", ;

wdStyleTypeParagraph)

WITH oMajorHeadingStyle

.BaseStyle = oBodyStyle

.Font.Size = 20

WITH .ParagraphFormat

.Alignment = wdAlignParagraphCenter

.SpaceAfter = 6 && leave a line after

.KeepWithNext = .T. && include at least one line of next

&& paragraph before new page

.KeepTogether = .T. && keep the whole paragraph together

ENDWITH

ENDWITH

* Minor heading is just big.

oMinorHeadingStyle = oDocument.Styles.Add("MinorHeading", ;

wdStyleTypeParagraph )

WITH oMinorHeadingStyle

.BaseStyle = oBodyStyle

.Font.Size = 16

ENDWITH

* Now create customer report

* First, our company info centered at the top

oRange.Style = oMajorHeadingStyle

oRange.InsertAfter("Automation Sample Company" + CR)

oRange.InsertAfter("Factory Blvd." + CR)

oRange.InsertAfter("Robotville, PA 19199" + CR)

* Now leave some blank space, then put info about this customer

oRange.Collapse( wdCollapseEnd )

oRange.End = oRange.End + 1 && to allow assignment to style

oRange.Style = oBodyStyle

oRange.InsertAfter(CR + CR)

* Use minor heading for customer id and name

* Put customer id in bold

oRange.Collapse( wdCollapseEnd )

oRange.End = oRange.End + 1 && to allow assignment to style

oRange.Style = oMinorHeadingStyle

oRange.InsertAfter(Customer_ID + ": " + TRIM(Company_Name) + CR)

oRange.Words[1].Font.Bold = .t.

* Regular body style for address info

oRange.Collapse( wdCollapseEnd )

oRange.End = oRange.End + 1 && to allow assignment to style

oRange.Style = oBodyStyle

oRange.InsertAfter(TRIM(Contact_Title) + ":" + TRIM(Contact_Name) ;

+ CR)

oRange.InsertAfter(TRIM(Address) + CR)

oRange.InsertAfter(TRIM(City) + " " + TRIM(Region) + ;

Postal_Code + CR)

oRange.InsertAfter(UPPER(TRIM(Country)) + CR )

* Extra line for spacing

oRange.InsertAfter( CR )

* Back to minor heading for phone number

oRange.Collapse( wdCollapseEnd )

oRange.End = oRange.End + 1 && to allow assignment to style

oRange.Style = oMinorHeadingStyle

oRange.InsertAfter( "Phone: " + TRIM(Phone) + CR)

* Fax number in regular body style

oRange.Collapse( wdCollapseEnd )

oRange.End = oRange.End + 1 && to allow assignment to style

oRange.Style = oBodyStyle

oRange.InsertAfter( "Fax: " + TRIM(Fax) + CR )

Note the use of the Words collection to bold only the customer id rather than the whole line. Figure 7 shows the resulting document in Word.

Figure 7. Using styles. Rather than formatting every item independently, styles let you define and name sets of formatting characteristics, then apply them uniformly within and across documents. Styles can be considered OOP for formatting.

In an example this small, it can be hard to see the point of creating and using styles. It s worth noting that the code in the example is divided almost exactly evenly between formatting the styles and putting the data into the document. Far more lines would have been needed to perform the same formatting without using styles. Furthermore, consider what would be needed to add more information for the same customer. Once the styles are defined, they can be used over and over. With 42 lines of code, we have three styles that can be applied wherever we need them, no matter how many more lines of code we write to send text to the document. In Chapter 5, "Intermediate Word," we ll look at creating templates, in which we can save our own styles for use in multiple documents.

 

Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved



Microsoft Office Automation with Visual FoxPro
Microsoft Office Automation with Visual FoxPro
ISBN: 0965509303
EAN: 2147483647
Year: 2000
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net