4.6 Output

Output

Producing attractive documents is a good start, but users usually want output of some sort, too. Word has a number of methods for producing output. In the interactive product, they re collected on the File menu in the Print Preview and Print menu options.

Printing

The PrintOut method of the Document object automates printing. It accepts a huge array of parameters. (This is one situation where named parameters start to look pretty good.)

Fortunately, all parameters are optional, and you won t need most of those parameters for normal processing. Unfortunately, the ones you re most likely to need are in the middle of the list rather than toward the front. The key parameters are shown here:

oDocument.PrintOut( lBackground, , nPrintRangeType, cFileName, cFrom, cTo, ,

cCopies, cPageRange, , lPrintToFile)

lBackground

Logical

Indicates whether or not to print in the background, continuing with other code while printing.

nPrintRangeType

Numeric

Which part of the document to print. (See the text.)

cFileName

Character

If printing to file, the filename, including path, of the destination file.

cFrom, cTo

Character

If nPrintRangeType is wdPrintFromTo, the beginning and end of the print range. (See the text.)

cCopies

Character

The number of copies to print.

cPageRange

Character

If nPrintRangeType is wdPrintRangeOfPages, the print range. (See the text.)

lPrintToFile

Logical

Indicates whether to print to a file.

Incredibly, there are still eight more parameters after these. The syntax shown reflects two different approaches to specifying the pages to be printed. The nPrintRangeType parameter determines which, if either, is used. The valid constant values are wdPrintAllDocument (0), wdPrintSelection (1), wdPrintCurrentPage (2), wdPrintFromTo (3), and wdPrintRangeOfPages (4). When wdPrintFromTo is passed, pass values for cFrom and cTo as the fifth and sixth parameters, respectively. Note that, for reasons known only to Microsoft, they re passed as characters. For example, to print three copies of pages 4 10 of a document referenced by oDocument, use:

oDocument.PrintOut( , , wdPrintFromTo, , "4", "10", , "3")

The wdPrintRangeOfPages choice lets you specify a single string and has more flexibility. In that case, pass a value for cPageRange as the eighth parameter it can include multiple comma-separated values, and each may include a range. For example, you could pass something like "3, 4-6, 12".

To print to a file, you specify both the filename and a flag that you re printing to file, like this:

oDocument.PrintOut( , , , "fileoutput", , , , , , , .t.)

The resulting file is ready to print, complete with printer codes. Keep in mind that the file is stored in Word s current directory by default, so it s a good idea to provide the full path.

Be forewarned that printing to file sets the Print to File check box in the Print dialog and leaves it set. Omitting that parameter in a subsequent call to PrintOut defaults to .T.; you have to explicitly pass .F. to print to the printer.

Creating envelopes

Word can also automatically create and print envelopes. To do so, use the document s Envelope object. This example assumes that oRange is a range containing the customer name and address in a mailing format. It bookmarks the address (see the "Bookmarks" section earlier in this chapter for an introduction to this technique for identifying part of a document) and asks Word to create and print the envelope.

oDocument.Bookmarks.Add("EnvelopeAddress", oRange)

oDocument.Envelope.PrintOut(.T., , ,.F., oWord.UserAddress)

First, we create a bookmark called EnvelopeAddress based on whatever is at oRange. The call to PrintOut tells Word to use whatever is at the EnvelopeAddress bookmark for the address, and to include the user s stored address (from the Tools|Options dialog s User Information page) as the return address.

The parameters for the PrintOut method of the Envelope object are different from those for the Document object. Here s a shortened version of the syntax:

oDocument.Envelope.PrintOut( lExtractAddress, cAddress, , lOmitReturnAddress,

cReturnAddress )

lExtractAddress

Logical

Indicates whether to extract the address from the EnvelopeAddress bookmark.

cAddress

Character

The address to use on the envelope. Ignored if lExtractAddress is .T.

lOmitReturnAddress

Logical

Indicates whether to omit the return address from the envelope.

cReturnAddress

Character

The return address to use on the envelope.

Additional parameters let you set the type and size of the envelope, as well as the printing orientation. Omitting those parameters uses the current settings.

Print preview

Perhaps all your users want is to see how the document will look when printed. That s easy. Just call the Document object s PrintPreview method. That switches Word to PrintPreview mode.

Of course, that s only useful if Word is visible. If Word is hidden, it doesn t do a bit of good. Making Word visible is as easy as setting the Application s Visible property to .T. Better yet, if you ve been doing everything in the background and now you re ready to show the user what you ve been up to, call PrintPreview, make Word visible, and then call Word s Activate method. That will bring Word to the front. Try it like this:

oDocument.PrintPreview && Get the document ready for the user to see

oWord.Visible = .t. && Show Word

oWord.Activate() && Bring it to the front

Regardless of whether you re keeping Word hidden most of the time or showing it all along, when you re done with Print Preview, you turn it off by calling the ClosePrintPreview method of the Document object.

 

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