4.7 Putting it all together

Putting it all together

Listing 3 shows a program (WordSample1.PRG in the Developer Download files available at www.hentzenwerke.com) that creates a document that lists all of Tasmanian Traders customers, organized by country. It demonstrates headers, footers, page numbers, shading, and borders. Figure 14 shows a preview of the resulting document.

Listing 3. Tasmanian Traders customer report.

#DEFINE wdHeaderFooterPrimary 1

#DEFINE wdGray25 16

#DEFINE wdStyleTypeParagraph 1

#DEFINE wdCaptionNumberStyleArabic 0

#DEFINE wdAlignPageNumberCenter 1

#DEFINE wdAlignParagraphCenter 1

#DEFINE wdCollapseEnd 0

#DEFINE wdLineStyleSingle 1

#DEFINE wdLineStyleNone 0

#DEFINE wdLineWidth050Point 4

#DEFINE CR CHR(13)

RELEASE ALL LIKE o*

PUBLIC oWord

LOCAL oDoc, oRange, cText, oHeaderStyle

oWord = CreateObject("Word.Application")

oWord.Visible = .T.

oDoc = oWord.Documents.Add()

* Create a style for the header text

oHeaderStyle = oDoc.Styles.Add("CenteredHeader", wdStyleTypeParagraph)

WITH oHeaderStyle

.BaseStyle = oDoc.Styles["Heading 1"]

.ParagraphFormat.Alignment = wdAlignParagraphCenter

ENDWITH

* Add a header

WITH oDoc.Sections[1].Headers[ wdHeaderFooterPrimary ]

oRange = .Range()

WITH oRange

.Text = "Tasmanian Traders"

.Style = oHeaderStyle

.Shading.BackgroundPatternColorIndex = wdGray25

ENDWITH

ENDWITH

* Add a centered page number in the footer

WITH oDoc.Sections[1].Footers[ wdHeaderFooterPrimary ].PageNumbers

.Add()

.NumberStyle = wdCaptionNumberStyleArabic

ENDWITH

oDoc.Sections[1].Footers[ wdHeaderFooterPrimary ].PageNumbers[1].Alignment = ;

wdAlignPageNumberCenter

* Now create some content for the document.

* Get a list of customers organized by country.

* Sort alphabetically within countries.

SELECT Company_Name, Country ;

FROM _SAMPLES+"\TasTrade\Data\Customer" ;

ORDER BY Country, Company_Name ;

INTO CURSOR CustomerByCountry

* Title for document

oRange = oDoc.Range()

oRange.InsertAfter("Customers By Country" + CR)

oRange.Style = oDoc.Styles("Heading 1")

oRange.Collapse( wdCollapseEnd )

* Loop through cursor. Put each country name bordered in heading 2 style.

* Then list each customer in that country.

LOCAL cCurrentCountry

cCurrentCountry = ""

cText = ""

SCAN

IF NOT (Country==cCurrentCountry)

* New country.

oRange.InsertAfter( cText + CR )

oRange.Style = oDoc.Styles("Normal")

cText = ""

oRange.Collapse( wdCollapseEnd )

WITH oRange

.InsertAfter( Country + CR )

.Style = oDoc.Styles("Heading 2")

.Borders.OutsideLineStyle = wdLineStyleSingle

.Borders.OutsideLineWidth = wdLineWidth050Point

ENDWITH

oRange.Collapse( wdCollapseEnd )

oRange.InsertAfter( CR )

oRange.Borders.OutsideLineStyle = wdLineStyleNone

ENDIF

cText = cText + Company_Name + CR

cCurrentCountry = Country

ENDSCAN

oRange.InsertAfter( cText )

oRange.Style = oDoc.Styles("Normal")

USE IN CustomerByCountry

USE IN Customer

oDoc.PrintPreview()

This chapter covered an incredible amount of material, and we re only warming up. Word has much more to offer. Stay tuned.

Figure 14. The customer report. The list of Tasmanian Traders customers demonstrates borders, shading, headers, footers, page numbers, and more.

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