Putting it all together
Listing 3
(WordSample2.PRG in the Developer Download files available at www.hentzenwerke.com) shows a program that creates an employee list for Tasmanian Traders. The list uses a table organized by country, with a heading for each country centered in the table. For each employee, it includes name, birth date, date of hire, and a photo. The notes stored for each employee are listed as endnotes to the document. Figure 17 and Figure 18 show two pages of the resulting document.Listing 3
. Building a complete document. This program creates an employee listing, using a table, endnotes, and graphics. The results are shown in Figure 17 and Figure 18.* This is the "putting it all together" example for Chapter 5.
* It demonstrates the use of tables, graphics, and endnotes.
* The program creates a document that shows all the TasTrade
* employees in a table organized by country.
#DEFINE CR CHR(13)
#DEFINE wdHeaderFooterPrimary 1
#DEFINE wdGray25 16
#DEFINE wdAlignParagraphCenter 1
#DEFINE wdCollapseEnd 0
#DEFINE wdWord9TableBehavior 1
#DEFINE wdAutoFitContent 1
#DEFINE wdTableFormatList7 30
#DEFINE wdCellAlignVerticalCenter 1
RELEASE ALL LIKE o*
PUBLIC oWord
LOCAL oDocument, oRange, oTable, oRow, nRow
LOCAL nCountries, nEmployees, cCountry
oWord = CreateObject("Word.Application")
oWord.Visible = .t.
oDocument = oWord.Documents.Add()
* Set font for Normal style
WITH oDocument.Styles["Normal"].Font
.Name = "Arial"
.Size = 12
ENDWITH
* Add a header
WITH oDocument.Sections[1].Headers[ wdHeaderFooterPrimary ]
oRange = .Range()
WITH oRange
.Text = "Tasmanian Traders"
.Style = oDocument.Styles["Heading 1"]
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Shading.BackgroundPatternColorIndex = wdGray25
ENDWITH
ENDWITH
* Open the data
OPEN DATA _SAMPLES + "TasTrade\Data\TasTrade"
* Run a query to collect the right data in the right order
SELECT Country, Title, First_Name, Last_Name, Birth_Date, ;
Hire_Date, Notes, Photo_File ;
FROM Employee ;
ORDER BY Country, Title, Last_Name, First_Name ;
INTO CURSOR Emps
nEmployees = _TALLY
* Add a heading
oRange = oDocument.Range()
WITH oRange
.Style = oDocument.Styles["Heading 1"]
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.InsertAfter("Employees" + CR + CR)
.Collapse( wdCollapseEnd )
.Style = oDocument.Styles["Normal"]
ENDWITH
* Figure out the number of different countries
SELECT Country, COUNT( * ) ;
FROM Emps ;
GROUP BY Country ;
INTO CURSOR Countries
nCountries = _TALLY
* Now add the table, with one Row for each Employee plus one for each Country
* Set up the table to resize itself based on the content of the cells.
oTable = oDocument.Tables.Add( oRange, nEmployees + nCountries + 1, 6, ;
wdWord9TableBehavior, wdAutoFitContent )
* Apply one of the built-in formats
oTable.AutoFormat( wdTableFormatList7, .T., .T., .T., .T., .T., .F., ;
.F., .F., .F.)
* Set up table heading row
WITH oTable.Rows[1]
.Range.Style = oDocument.Styles[ "Heading 2" ]
.Cells[1].Range.InsertAfter("Title")
.Cells[2].Range.InsertAfter("First Name")
.Cells[3].Range.InsertAfter("Last Name")
.Cells[4].Range.InsertAfter("Birthdate")
.Cells[5].Range.InsertAfter("Hired")
* Set up this row to be repeated on subsequent pages
.HeadingFormat = .T.
ENDWITH
* Now loop through the employee data, sending it to the table.
* Keep track of the current country. For each new country,
* create a "header" row in the table.
cCountry = ""
nRow = 1
SELECT Emps
SCAN
* Grab the current row
nRow = nRow + 1
oRow = oTable.Rows[ nRow ].Range()
IF NOT (Country == cCountry)
* Change of country
oRow.Cells.Merge()
oRow.Style = oDocument.Styles[ "Heading 3" ]
oRow.ParagraphFormat.Alignment = wdAlignParagraphCenter
oRow.InsertAfter( Country )
cCountry = Country
nRow = nRow + 1
oRow = oTable.Rows[ nRow ].Range()
ENDIF
* Now insert the data for this employee
WITH oRow
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
.Cells[1].Range.InsertAfter( Title )
.Cells[2].Range.InsertAfter( First_Name )
.Cells[3].Range.InsertAfter( TRIM(Last_Name) )
* Add an endnote for the memo information
oDocument.EndNotes.Add(.Cells[3].Range(), , Notes )
.Cells[4].Range.InsertAfter( Birth_Date )
.Cells[5].Range.InsertAfter( Hire_Date )
* Add the picture in the last cell
oPicture = oDocument.InlineShapes.AddPicture ( ;
_SAMPLES + "TasTrade\" + Photo_File, ;
.t., .f., .Cells[6].Range() )
ENDWITH
WITH oPicture
.ScaleHeight = 50
.ScaleWidth = 50
ENDWITH
ENDSCAN
oDocument.PrintPreview()
RETURN
Figure 17
. Tasmanian Traders employee report. The first page includes a heading then jumps right into the table. You can put graphics in a table as well as text, and not every row has to have the same number of columns.Figure 18
. Using endnotes for additional information. Although the information from the employee notes field could have been placed in the table, putting it in endnotes gives the table an uncluttered look.With the material in this chapter and the preceding one, you can handle most everyday word processing tasks. Now we move on to the more unusual tasks the things you don t see every day but still might be asked to make Word do.
Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved