Recipe 5.4 Programmatically Change Printer Options

5.4.1 Problem

You've tried using SendKeys to change printing options in the File Setup Page dialog, but this really isn't satisfactory. Sometimes it works and sometimes it doesn't, depending on the circumstances and the printer driver that's loaded. Is there some way to modify printer options without using SendKeys?

5.4.2 Solution

Windows makes many of the printer driver settings available to applications, including the number of copies, page orientation, and page size. Starting with Access 2002, it's easy to retrieve and modify these values, using the Printer property of forms and reports. This solution focuses on the print settings features of the Printer object and demonstrates how to read and write values in the Printer object.

To be able to modify printer settings for reports or forms in your own applications, follow these steps:

  1. Open the report in either preview or design view. (If you want to make your changes persistent, open the report in design view. If you want to apply changes for just this particular instance, open it in preview mode.)

  2. Modify some of the properties of the Printer object provided by your form or report. For example, the sample form works with a small subset of the available printer-specific properties, using code like this:

    DoCmd.OpenReport strReport, View:=acViewPreview
    With Reports(strReport).Printer
        Me.txtCopies = .Copies
        Me.grpOrientation = .Orientation
        Me.grpPaperSize = .PaperSize
    End With
  3. When you're done working with the properties, write them back to the report's Printer object, using code like this (from the sample form):

    strReport = Me.cboReportList
    With Reports(strReport).Printer
        .Copies = Me.txtCopies
        .Orientation = Me.grpOrientation
        .PaperSize = Me.grpPaperSize
    End With

For an example, load and run the form frmPrintSettings in 05-04.MDB. Figure 5-4 shows the sample form in action. This form allows you to choose a report from a combo box. Once you've made your choice, the form loads the report in preview mode and retrieves the number of copies, page size, and page orientation from the report's Printer property. You can change any of these values; once you click Save Settings, the form will write the values back to the report's Printer property and the changes will display immediately in the preview window.

Figure 5-4. frmPrintSettings shows print information for rptReport1
figs/acb2_0504.gif

5.4.3 Discussion

Each form and report has a Printer property that holds a reference to a Printer object for that form or report. By setting the properties of a form/report's Printer object, you can control how the form/report will print. Table 5-2 lists the object's properties, along with their possible values. None of these properties is read-only. Table 5-3 shows a list of all the defined paper sizes. You can use one of these constants in the PaperSize property to set a new paper size. Table 5-4 shows possible values for the PaperBin property of the Printer object.

Table 5-2. Properties of the Printer object

Field name

Contains

Data type

Values

Orientation

Paper orientation

AcPrintOrientation

acPRORLandscape or acPRORPortrait

PaperSize

Size of the physical page to print on

AcPrintPaperSize

A value from Table 5-3 (depending on which paper sizes the printer supports)

Copies

If the printing device supports multiple copies, the number of copies to be printed

Long  

PaperBin

Default bin from which paper is to be fed

AcPrintPaperBin

A value from Table 5-4

PrintQuality

Printer resolution

AcPrintObjQuality acPRPQDraft, acPRPQHigh, acPRPQLow, or acPRPQMedium

ColorMode

Color usage, if the printer supports color printing

AcPrintColor acPRCMColor or acPRCMMonochrome

Duplex

Duplex usage, if the printer supports duplex printing

AcPrintDuplex acPRDPHorizontal, acPRDPSimplex, or acPRDPVertical

Table 5-3. Constants and descriptions for the PaperSize property

Constant

Value

Description

 acPRPS10X14

16

10 x 14 in

 acPRPS11X17

17

11 x 17 in

 acPRPSA3

8

A3 (297 x 420 mm)

 acPRPSA4

9

A4 (210 x 297 mm)

 acPRPSA4SMALL

10

A4 Small (210 x 297 mm)

 acPRPSA5

11

A5 (148 x 210 mm)

 acPRPSB4

12

B4 (250 x 354 mm)

 acPRPSB5

13

B5 (182 x 257 mm)

 acPRPSCSHEET

24

C size sheet (17 x 22 in)

 acPRPSDSHEET

25

D size sheet (22 x 34 in)

 acPRPSEnv10

20

Envelope #10 (4.125 x 9.5 in)

 acPRPSEnv11

21

Envelope #11 (4.5 x 10.375 in)

 acPRPSEnv12

22

Envelope #12 (4.25 x 11 in)

 acPRPSEnv14

23

Envelope #14 (5 x 11.5 in)

 acPRPSEnv9

19

Envelope #9 (3.875 x 8.875 in)

 acPRPSEnvB4

33

Envelope B4 (250 x 353 mm)

 acPRPSEnvB5

34

Envelope B5 (176 x 250 mm

 acPRPSEnvB6

35

Envelope B6 (176 x 125 mm)

 acPRPSEnvC3

29

Envelope C3 (324 x 458 mm)

 acPRPSEnvC4

30

Envelope C4 (229 x 324 mm)

 acPRPSEnvC5

28

Envelope C5 (162 x 229 mm)

 acPRPSEnvC6

31

Envelope C6 (114 x 162 mm)

 acPRPSEnvC65

32

Envelope C65 (114 x 229 mm)

 acPRPSEnvDL

27

Envelope DL (110 x 220 mm)

 acPRPSEnvItaly

36

Envelope (110 x 230 mm)

 acPRPSEnvMonarch

37

Envelope Monarch (3.875 x 7.5 in)

 acPRPSEnvPersonal

38

6-3/4 Envelope (3.625 x 6.5 in)

 acPRPSESheet

26

E size sheet (34 x 44 in)

 acPRPSExecutive

7

Executive (7.25 x 10.5 in)

 acPRPSFanfoldLglGerman

41

German Legal Fanfold (8.5 x 13 in)

 acPRPSFanfoldStdGerman

40

German Std Fanfold (8.5 x 12 in)

 acPRPSFanfoldUS

39

US Std Fanfold (14.875 x 11 in)

 acPRPSFolio

14

Folio (8.5 x 13 in)

 acPRPSLedger

4

Ledger (17 x 11 in)

 acPRPSLegal

5

Legal (8.5 x 14 in)

 acPRPSLetter

1

Letter (8.5 x 11 in)

 acPRPSLetterSmall

2

Letter Small (8.5 x 11 in)

 acPRPSNote

18

Note (8.5 x 11 in)

 acPRPSQuarto

15

Quarto (215 x 275 mm)

 acPRPSStatement

6

Statement (5.5 x 8.5 in)

 acPRPSTabloid

3

Tabloid (11 x 17 in)

 acPRPSUser

256

User-defined

Table 5-4. Constants and descriptions for the PaperBin property

Constant

Value

Description

 acPRBNAuto

7

Automatic bin

 acPRBNCassette

14

Cassette bin

 acPRBNEnvelope

5

Envelope bin

 acPRBNEnvManual

6

Envelope manual bin

 acPRBNLargeCapacity

11

Large-capacity bin

 acPRBNLargeFmt

10

Large-format bin

 acPRBNLower

2

Lower bin

 acPRBNManual

4

Manual bin

 acPRBNMiddle

3

Middle bin

 acPRBNSmallFmt

9

Small-format bin

 acPRBNTractor

8

Tractor bin

 acPRBNUpper

1

Upper bin

 acPRBNFormSource

15

Form source

The sample form opens reports in preview mode and allows you to modify and view printer-specific properties, then print the report. If you want to modify the design properties for a report, you'll need to open it in design view, modifying the call to the DoCmd.OpenReport method in the code, like this:

DoCmd.OpenReport strReport, View:=acViewPreview

When you're done, save the report using code like this:

DoCmd.Close acReport, "YourReportName", acSaveYes

Although Access makes it easy to work with printer settings, the Printer object is missing some important features. For example, although you can select acPRPSUser for the PaperSize property, you cannot define your own sizes (making this option effectively useless).