Method

OpenPrinter


Warning

This item was deprecated in version 2019r2. Please use PrinterSetup.OpenPrinter as a replacement.

Description

Returns a Graphics object in order to print to the currently selected printer.

Usage

result = OpenPrinter([ps As PageSetup])

Part

Type

Description

result

Graphics

A graphics object that represents the page to be printed.

PageSetup

PrinterSetup

Optional. A PrinterSetup object whose properties will be used (like page orientation, scale, etc.) when printing. Although this is optional, you will mostly likely always want to provide a PrinterSetup so that you have access to drawing resolutions.

Notes

The OpenPrinter function returns a Graphics object. The various drawing routines can then be used to draw into the Graphics object and will be sent to the printer for printing.

Although the pageSetup parameter is optional, in order to get the correct horizontal and vertical resolutions for drawing and positioning you will need to provide a PrinterSetup. You can do this without prompting the user with a dialog like this:

Dim p As New PageSetup
g = OpenPrinter(p)
Dim horizontalDPI = p.HorizontalResolution
Dim verticalDPI = p.VerticalResolution

Sample code

This code prints "Hello World" to the currently selected printer:

Dim p As New PageSetup
Dim g As Graphics
g = OpenPrinter(p)
If g <> Nil Then
  ' Draw text 1 inch across and 1 inch down
  g.DrawString("Hello World", p.HorizontalResolution, p.VerticalResolution)
End If

This code uses the Page Setup properties for the printer chosen by the user by the PageSetup dialog:

Dim ps As New PrinterSetup
If ps.PageSetupDialog Then
  Dim g As Graphics
  g = OpenPrinter(ps)
  If g <> Nil Then
    ' Draw text 1 inch across and 1 inch down
    g.DrawString("Hello World", ps.HorizontalResolution, ps.VerticalResolution)
  End If
End If

Compatibility

All project types on all supported operating systems.

See also

Graphics, PrinterSetup classes; ShowPrinterDialog function.