1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| Public Sub PrintReportToPrinter(ReportName As String, id As Long)
Dim db As DataBase
Dim rs As Recordset
Dim rpt As Report
Dim prtr As Printer
Set db = CurrentDb
On Error GoTo ShowPrinters_Err
Set Application.Printer = Nothing
If EvaluateField(Forms!NavBar!Text_depotid) Then
'Set rs = db.OpenRecordset("SELECT * FROM planisto_transport_imprimantes WHERE depotid = 5 and ReportName = '" & ReportName & "'")
Set rs = db.OpenRecordset("SELECT * FROM planisto_transport_imprimantes WHERE depotid = " & Forms!NavBar!Text_depotid & " and ReportName = '" & ReportName & "'")
'DoCmd.OpenReport stDocName, View:=acViewPreview, WindowMode:=acHidden
'Set Reports(stDocName).Report.Printer = Application.Printers(1)
strPrinter = rs("printername")
strPrinter2 = rs("printername2")
Set prtr = Application.Printers(strPrinter)
GoTo Next5
Error5:
Set prtr = Application.Printers(strPrinter2)
Next5:
If EvaluateField(rs("printerdrawer")) Then
'Set prtr = Application.Printers(strPrinter)
With prtr
.PaperBin = rs("printerdrawer")
.Copies = rs("copies")
.Duplex = acPRDPSimplex
'.Duplex = acPRDPHorizontal
'.Duplex = acPRDPVertical
End With
Else
'Set prtr = Application.Printers(strPrinter)
With prtr
.PaperBin = acPRBNAuto
.Copies = rs("copies")
.Duplex = acPRDPSimplex
'.Duplex = acPRDPHorizontal
End With
End If
'Set the default printer's paper bin to manual and paper size to legal
'prtr.PaperBin = acPRBNManual
'prtr.PaperSize = acPRPSLegal
Set Application.Printer = prtr
'open your report in hidden view to aplly print settings
If id > 0 Then
DoCmd.OpenReport ReportName, acViewPreview, , "[Num_tour] =" & id, acHidden
Else
DoCmd.OpenReport ReportName, acViewPreview, , , acHidden
End If
Msg = "There are " & Reports(ReportName).Pages & " Pages to be printed."
ipages = Reports(ReportName).Pages
Set rpt = Reports(ReportName)
rpt.Printer.PaperBin = prtr.PaperBin
rpt.Printer.Copies = prtr.Copies
rpt.Printer.Duplex = prtr.Duplex
'Print Report
DoCmd.OpenReport ReportName
'Set the Printer property of the report to the Application.Printer object
Set rpt.Printer = prtr
'now open the report in normal view to print
'DoCmd.OpenReport ReportName, acViewNormal, , "[Num_tour] =" & id
'DoCmd.PrintOut acPages, , , , 3, False
'DoCmd.Close acReport, ReportName, acSaveNo
Set rpt = Nothing
DoCmd.Close acReport, ReportName, acSaveNo
Set Application.Printer = Nothing
End If
ShowPrinters_End:
Exit Sub
ShowPrinters_Err:
If Err.Number = 5 Then
GoTo Error5
Else
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:="Error Number " & Err.Number & " Occurred"
Resume ShowPrinters_End
End If
End Sub |
Partager