4.5.1 Setting up pages

Setting up pages

The Page Setup dialog on Word s File menu tells Word what kind of paper to expect, where to get it, and how to lay the document out on that paper once it finds it. The dialog s controls specify the paper source (default paper tray, alternate tray, manual feed, and so forth), paper type (letter, legal, A4, and so on), and orientation of the page (portrait or landscape). The dialog is also used to determine the size of the margins, headers, and footers, to specify whether the first page of the document has a different header and footer than the rest and whether odd and even pages have different headers and footers. There are actually a lot more options there, too. Repeating our theme, Word is amazingly powerful. Figure 3 shows the Margins page of the Page Setup dialog.

Figure 3. The Page Setup dialog. This dialog lets you specify margins, paper size and orientation, whether headers and footers are the same or different on the first page and on odd and even pages, and much more.

The PageSetup object is the Automation object that parallels the dialog. It has an assortment of properties that handle the various options available. Unlike many of Word s objects, most of PageSetup s properties do not refer to other objects. They re simply values, making it a little easier to work with than many of the others. (A note of caution: although the other Office applications have objects named PageSetup, each Office application s object is distinct; they can t be used interchangeably.)

Table 2 shows the most common properties of the PageSetup object, along with constant values for commonly used settings.

Table 2. PageSetup properties. The PageSetup object mimics the Page Setup dialog on Word s File menu.

Property

Type

Description

PaperSize

Numeric

The type of paper to use for the document, such as legal, letter, A4, and so on.

wdPaperLetter

2

wdPaperLegal

4

wdPaperA4

7

wdPaper11x17

1

 

FirstPageTray

Numeric

The source for the first sheet of paper for the document. The first sheet is handled separately to allow, for example, letterhead for page one of a letter followed by plain paper for the rest.

wdPrinterDefaultBin

0

wdPrinterUpperBin

1

wdPrinterLowerBin

2

wdPrinterManualFeed

4

wdPrinterEnvelopeFeed

5

wdPrinterAutomaticSheetFeed

7

 

OtherPagesTray

Numeric

The source for sheets of paper other than the first. Uses the same constants as FirstPageTray.

Orientation

Numeric

The orientation of the paper.

wdOrientPortrait

0

wdOrientLandscape

1

 

TopMargin, BottomMargin

Numeric

The vertical margins of the page, in points.

LeftMargin, RightMargin

Numeric

The horizontal margins of the page, in points.

VerticalAlignment

Numeric

The vertical position of the text on the page.

wdAlignVerticalTop

0

wdAlignVerticalCenter

1

wdAlignVerticalJustify

2

wdAlignVerticalBottom

3

 

DifferentFirstPageHeaderFooter

Logical or Numeric

Indicates whether the first page of the document has different headers and footers than the rest of the document. (Numeric only if undefined.)

OddAndEvenPagesHeaderFooter

Logical or Numeric

Indicates whether odd pages and even pages have different headers and footers. (Numeric only if undefined.)

Like many other measurement-related properties, the margin settings expect points rather than what s shown in the Page Setup dialog. To make specifying measurements easier, the Word Application object has a number of conversion methods, including InchesToPoints, CentimetersToPoints, PointsToInches, PointsToCentimeters, and quite a few more.

The following example creates a document, centers it vertically on the page, gives it different odd and even page headers and footers, and sets the margins to one inch everywhere but at the bottom, where 0.75" is used.

* Set up a document with custom margins, different odd and

* even headers and footers, and center alignment

#DEFINE wdAlignVerticalCenter 1

oDocument = oWord.Documents.Add()

WITH oDocument.PageSetup

.VerticalAlignment = wdAlignVerticalCenter

.OddAndEvenPagesHeaderFooter = .T.

* Set up .75 bottom margin and 1-inch

* top, left and right margins.

LOCAL nThreeQuartersInPoints, nInchInPoints

nThreeQuartersInPoints = oWord.InchesToPoints( .75 )

nInchInPoints = oWord.InchesToPoints( 1 )

.BottomMargin = nThreeQuartersInPoints

.TopMargin = nInchInPoints

.LeftMargin = nInchInPoints

.RightMargin = nInchInPoints

ENDWITH

Since the number of points to the inch (or to the centimeter) never changes, you may prefer to perform these conversions yourself. You can define your own constants and perform the arithmetic in native VFP code rather than calling Automation methods to do it. Here s alternative code for the margin setting portion of the preceding example:

* ALTERNATE METHOD: replaces the seven lines of code, above:

#DEFINE autoInchesToPoints 72

.BottomMargin = 0.75 * autoInchesToPoints

.TopMargin = 1.00 * autoInchesToPoints

.LeftMargin = 1.00 * autoInchesToPoints

.RightMargin = 1.00 * autoInchesToPoints

Our tests show a speed improvement of about two orders of magnitude (that s about 100 times faster) doing the conversions ourselves. For an occasional computation, it doesn t really matter, but if you need to convert between points and other units hundreds or thousands of times, it s definitely worth using native VFP code and remembering to include the right constants everywhere.

The PageSetup object has a number of other, more obscure properties, not shown in Table 2. Though the descriptions for several properties shown in Table 2 refer to the document as a whole, each Section of a document can have its own PageSetup object whose properties apply only within that section.

PageSetup has only two methods. TogglePortrait switches Orientation between portrait and landscape. It s equivalent to setting the Orientation property directly to the opposite of its current setting. SetAsDefaultTemplate makes the current PageSetup the default for this document and for all new documents based on the active template. It s like clicking the Default button in the Page Setup dialog. (Watch out! The name of that method implies that it creates a default template. It doesn t. It creates default settings for the current template.)

 

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