Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to setup printers + properties in VB

Common Formulas

How to setup printers + properties in VB

by  cturland  Posted    (Edited  )
Here is a example of how to setup printers for reports in runtime and how to use Devmode to set its properties. First a list box is populated with all the available printers to the computer and allows the user to select which printer to print from and which printer bin to use.......

Private Sub Form_Load()
Dim PrintData As Printer

For Each PrintData In Printers
' Add printer name and port to list
lstPrintList.AddItem PrintData.DeviceName & " at " & PrintData.Port
' Check for default printer
If PrintData.DeviceName = Printer.DeviceName Then defprinterpos% = lstPrintList.NewIndex
lstPrintList.ListIndex = defprinterpos%
Next

End Sub

Private Sub cmdPrintReport_Click()
Dim PrtSet As crDEVMODE ' Declare a DevMode

'Get the printer selected in the index
curprinter% = lstPrintList.ListIndex

'Find the report
reportFilePath = txtReportPath.Text

'Open the print engine
Result% = PEOpenEngine()

'Setup PrtSet settings - Only using Orientation and DefaultSource
With PrtSet
.dmFields = &H200 Or &H1 'dmFields has to be set to what properties are being used in DEVMODE
.dmDefaultSource = Int(txtBinNo.Text)
.dmOrientation = DMORIENT_LANDSCAPE
End With

'Open the report file for printing
jobNo% = PEOpenPrintJob(reportFilePath)

'===================Setup Printer==================
PrtDriver$ = Printers(curprinter%).driverName & vbNullChar

PrtName$ = Printers(curprinter%).DeviceName & vbNullChar

PrtPort$ = Printers(curprinter%).Port & vbNullChar

[Color Red]'crPESelectPrinter function has to be used instead of PESelectPrinter function in VB due to VB32bit using 4-byte alignment whereas the CR print engine uses 1-byte alignment. The crPESelectPrinter function calls PESelectPrinter in a C++ 1-byte alignment enviroment, bypassing VB.[/color]

Response% = crPESelectPrinter(jobNo%, PrtDriver$, PrtName$, PrtPort$, PrtSet)
'==================================================

'Print the report and close the printer
Response% = PEOutputToPrinter(jobNo%, 1)
Response% = PEStartPrintJob(jobNo%, True)
Response% = PEClosePrintJob(jobNo%)
PECloseEngine

End Sub
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top