How Can I Change The Print Device For A Report Using Code

  • Thread starter Thread starter jbonacorda
  • Start date Start date
J

jbonacorda

Guest
How Can I Change The Print Device For A Report Using Code?

I am trying to change the print device (using PrtDev in VBA) for a report in one of my applications. I am able to change the paper size using the following sub -

Option Compare Database
Option Explicit
'*********************************************
' Author: Jim Bonacorda, MIS
' Phone: 732-721-5544 ext 3166
' E-mail: jbonacorda@sabert.com
' Created: 11/20/2002
' The purpose of this module is to manage the
' printer settings for reports in this application
'*********************************************
Const MOD_NAME$ = "Module modPrinterMgmt"

Type str_DEVMODE
RGB As String * 94
End Type

Type type_DEVMODE
strDeviceName As String * 16
intSpecVersion As Integer
intDriverVersion As Integer
intSize As Integer
intDriverExtra As Integer
lngFields As Long
intOrientation As Integer
intPaperSize As Integer
intPaperLength As Integer
intPaperWidth As Integer
intScale As Integer
intCopies As Integer
intDefaultSource As Integer
intPrintQuality As Integer
intColor As Integer
intDuplex As Integer
intResolution As Integer
intTTOption As Integer
intCollate As Integer
strFormName As String * 16
lngPad As Long
lngBits As Long
lngPW As Long
lngPH As Long
lngDFI As Long
lngDFr As Long
End Type

Sub sSwitchPaperSizeToLegal(rptName As String)
'The purpose of this sub is to set the page size
'for a report to legal
Const SUB_NAME$ = "sSwitchPaperSizeToLegal"
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim rpt As Report

'Open report in design view.
DoCmd.OpenReport rptName, acDesign 'Open report in Design view.
Set rpt = Reports(rptName)
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode ' Get current DEVMODE structure.
DevString.RGB = strDevModeExtra
LSet DM = DevString
DM.lngFields = DM.lngFields Or DM.intPaperSize Or DM.intPaperLength Or DM.intPaperWidth
DM.intPaperSize = 5 'legal
DM.intPaperWidth = 8.5 * 254 '8.5 inches
DM.intPaperLength = 14 * 254 '14 inches
LSet DevString = DM 'Update property.
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
DoCmd.Close acReport, rptName, acSaveYes

End Sub

I modeled the print device code from the above sub, but I am running into trouble trying to change the printer -

Sub sSwitchToOfficePrinter(rptName As String)
'*************************************************
'The purpose of this sub is to change the printer
'for a report to the printer in the Sayreville
'Office
'*************************************************
Const SUB_NAME$ = "sSwitchToOfficePrinter"
Dim DevString As str_DEVMODE
Dim DM As type_DEVMODE
Dim strDevModeExtra As String
Dim rpt As Report

'Open report in design view.
DoCmd.OpenReport rptName, acDesign 'Open report in Design view.
Set rpt = Reports(rptName)
If Not IsNull(rpt.PrtDevMode) Then
strDevModeExtra = rpt.PrtDevMode 'Get current DEVMODE structure.
DevString.RGB = strDevModeExtra
LSet DM = DevString
DM.lngFields = DM.lngFields
DM.strDeviceName = "\\Sabert_GX110\Canon iR600-550-60 PCL" 'Printer in Sayreville office
LSet DevString = DM 'Update property.
Mid(strDevModeExtra, 1, 94) = DevString.RGB
rpt.PrtDevMode = strDevModeExtra
End If
DoCmd.Close acReport, rptName, acSaveYes

End Sub

Any suggestions will be appreciated.


Best regards,
Jim Bonacorda
 
Last edited:
I tried the same thing and in the end gave up.
I came up with several alternatives

1.. Created several copies of the same report and set the print options diferent on each. Then made several buttons on the form to print from. Then I changed that to a popup form with the buttons on it.

The other alternative is to open the Print Options box

I found this code here but haven't tested it :D

Set the code on the command button to docmd.openreport “ReportName”,acviewprintpreview


Add the following to the "On Activate" event of the report.

Private Sub Report_Activate()
On Error GoTo Err_Report_Activate
Dim strMsg As String
Dim strTitle As String
strMsg = "There were No Records Returned." & vbCrLf & _
"Print has been Canceled."
strTitle = " No Records Returned"

If Me.Report.HasData Then
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, Me.Name

Else
MsgBox strMsg, vbInformation + vbOKOnly, strTitle
DoCmd.Close acReport, Me.Name

End If

Err_Report_Activate:
Resume Next
DoCmd.Close acReport, Me.Name

End sub

HTH
Dave
 

Users who are viewing this thread

Back
Top Bottom