Avatar of wehoit
 asked on

How can I Set Printer Page size (based on available sizes) without Printer Dialog

I need to be able to set the paper size for a printer without using the Printer Dialog box, based on the available paper sizes for the selected printer.

For example, in code, I'd like to do the following:

1) Retrieve the paper sizes available on the active printer (Printer)
2) Loop through the available size names to match a requested name (e.g. if papersize="Envelope #9")
3) When (if) I find a match, set the printer to the matched paper size.

So that if the printer default is "Letter" but I want to print to "Envelope #9", I can set the active Printer, loop through the paper sizes, and when I find "Envelope #9", set it and then print.

Basically, I want to mimic the Printer Dialog, but all in code.

*****  What's most important is that if I happen across a printer with non-standard page sizes, I should be able to pick them from a 'list', almost as if I were using the printer dialog
For example, a printer may use "Architectural D" wihch is not a windows standard size, but I should still be able to match it from the available page sizes and then set it on the printer.

Please provide code which does what I need - do not point me to a link which has some of the parts...after all - you're the expert!

Visual Basic Classic

Avatar of undefined
Last Comment

8/22/2022 - Mon


Neither link has the kind of information I'm after.
Besides, I asked for code, not links.
Thanks for trying though.

This is how to get the possible pagesizes:

Option Explicit
Private Declare Function DeviceCapabilities Lib "winspool.drv" Alias "DeviceCapabilitiesA" (ByVal lpDeviceName As String, ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, lpDevMode As Any) As Long
Private Const DC_PAPERS = 2

Private Sub Command1_Click()
    Dim i As Integer
    Dim lRet As Long
    Dim iPaperSizes() As Integer
    lRet = DeviceCapabilities(Printer.DeviceName, "LPT1", DC_PAPERS, ByVal 0&, ByVal 0&)
    ReDim iPaperSizes(1 To lRet) As Integer
    Call DeviceCapabilities(Printer.DeviceName, "LPT1", DC_PAPERS, iPaperSizes(1), ByVal 0&)
    For i = 1 To lRet
        List1.AddItem Str$(iPaperSizes(i))

End Sub

The returned number means:

1 Letter, 8 1/2 x 11 in.  
2 Letter Small, 8 1/2 x 11 in.  
3 Tabloid, 11 x 17 in.  
4 Ledger, 17 x 11 in.  
5 Legal, 8 1/2 x 14 in.  
6 Statement, 5 1/2 x 8 1/2 in.  
7 Executive, 7 1/2 x 10 1/2 in.  
8 A3, 297 x 420 mm  
9 A4, 210 x 297 mm  
10 A4 Small, 210 x 297 mm  
11 A5, 148 x 210 mm  
12 B4, 250 x 354 mm  
13 B5, 182 x 257 mm  
14 Folio, 8 1/2 x 13 in.  
15 Quarto, 215 x 275 mm  
16 10 x 14 in.  
17 11 x 17 in.  
18 Note, 8 1/2 x 11 in.  
19 Envelope #9, 3 7/8 x 8 7/8 in.  
20 Envelope #10, 4 1/8 x 9 1/2 in.  
21 Envelope #11, 4 1/2 x 10 3/8 in.  
22 Envelope #12, 4 1/2 x 11 in.  
23 Envelope #14, 5 x 11 1/2 in.  
24 C size sheet  
25 D size sheet  
26 E size sheet  
27 Envelope DL, 110 x 220 mm  
29 Envelope C3, 324 x 458 mm  
30 Envelope C4, 229 x 324 mm  
28 Envelope C5, 162 x 229 mm  
31 Envelope C6, 114 x 162 mm  
32 Envelope C65, 114 x 229 mm  
33 Envelope B4, 250 x 353 mm  
34 Envelope B5, 176 x 250 mm  
35 Envelope B6, 176 x 125 mm  
36 Envelope, 110 x 230 mm  
37 Envelope Monarch, 3 7/8 x 7 1/2 in.  
38 Envelope, 3 5/8 x 6 1/2 in.  
39 U.S. Standard Fanfold, 14 7/8 x 11 in.  
40 German Standard Fanfold, 8 1/2 x 12 in.  
41 German Legal Fanfold, 8 1/2 x 13 in.  
256 User-defined

Hope this is what your looking for:

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

I've found my solution to this problem - from other sources.

I've attached the basic code.

It first queries the printer for all the PaperSizes, then queries the printer
for the PaperNames.
The PaperSizes are the Windows Form Size integer values (i.e. Letter=1, Tabloid=3, etc).

Fortunately, Windows returns the PaperNames and PaperSizes in the same order, so the first name has a value of the first size, etc.

Thanks to those who replied.

Sorry - I forgot to give a link to solution code:


View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.