How to change printer via VBA?

J

John Noble

I'd like to write a procedure that will set the users printer to an
installed pdf writer, and then use the PrintOut method to print the
specified object. Is this possible? Can I change the Default Printer via
code and then change it back after the PrintOut statement?

Thanks,
John Noble
Network Office Clearinghouse
 
V

Van T. Dinh

(Please post OS and Access version in the future).

In AXP, there is a Printer Object you can manage programmatically.
 
J

Jose Hernandez

In Access 2002 use the Printer Object
OR
Access97 (Also works in Access 2002) use PrtDevNames


Careful with the Line Wraps
'---------------------START-----------------------------------------
' Author: Jose Hernandez
Option Compare Database
Option Explicit

Type PrtDevNameStr 'See PrtDevNames property in Help
RGB As String * 104
End Type

Type PrtDevModeStr 'See PrtDevMode property in Help
RGB As String * 68
End Type

Type udtAccPrinterDEV 'Printer info from Reports
Source As String
DevName As PrtDevNameStr
DevMode As PrtDevModeStr
End Type

Private Function GetPrinterInfoEX(ByVal strSrc As String) As
udtAccPrinterDEV
On Error GoTo Proc_Error
'PURPOSE: Get DevNames & DevMode for access report
'strSrc = name of report with the printer settings.
DoCmd.OpenReport strSrc, acDesign 'Open color settings report
With GetPrinterInfoEX
.DevName.RGB = Reports(strSrc).PrtDevNames 'Get DevNames Structure
.DevMode.RGB = Reports(strSrc).PrtDevMode 'Get DevNames Structure
.Source = strSrc
End With
DoCmd.Close acReport, strSrc, acSaveNo
Proc_Exit:
If IsReportLoaded(strSrc) = True Then DoCmd.Close acReport, strSrc,
acSaveNo
Exit Function
Proc_Error:
MsgBox Err.Description
Resume Proc_Exit
End Function

Function IsReportLoaded(ByVal strReport As String) As Integer
' Returns True if the specified report is loaded.
IsReportLoaded = CBool(SysCmd(acSysCmdGetObjectState, acReport,
strReport) <> 0)
End Function

Function Print2AnyPrinter(strReport As String, strFilter As String,
strPrinterSettings As String) As Long
On Error GoTo Proc_Error
'Returns: 0 = Success!
'strReport = Report you want to print
'strFilter = Your Filter If you do not have one then pass "" as the value
'strPrinterSettings = The Report that has the printer settings! (Create a
new report and Set its Default Printer.)
'ISSUES: The reports MUST be opened in Design View on order to set the
PrtDevNames!

Dim udtPrinterDEV As udtAccPrinterDEV
Dim lngRetVal As Long

udtPrinterDEV = GetPrinterInfoEX(strPrinterSettings)

DoCmd.OpenReport strReport, acViewDesign
Reports(strReport).PrtDevNames = udtPrinterDEV.DevName.RGB
If Len(strFilter & "") > 0 Then
Reports(strReport).FilterOn = True
Reports(strReport).Filter = strFilter
End If
DoCmd.OpenReport strReport, acViewPreview
DoCmd.PrintOut acPrintAll 'Print Report
Proc_Exit:
If IsReportLoaded(strReport) = True Then DoCmd.Close acReport,
strReport, acSaveNo
Exit Function
Proc_Error:
MsgBox Err.Description
Print2AnyPrinter = Err.Number
Resume Proc_Exit
End Function
'-------------------------------END-------------------------------------

HTH

Jose
 
J

John Noble

Access 2000??

Thanks,
John Noble

Jose Hernandez said:
In Access 2002 use the Printer Object
OR
Access97 (Also works in Access 2002) use PrtDevNames


Careful with the Line Wraps
'---------------------START-----------------------------------------
' Author: Jose Hernandez
Option Compare Database
Option Explicit

Type PrtDevNameStr 'See PrtDevNames property in Help
RGB As String * 104
End Type

Type PrtDevModeStr 'See PrtDevMode property in Help
RGB As String * 68
End Type

Type udtAccPrinterDEV 'Printer info from Reports
Source As String
DevName As PrtDevNameStr
DevMode As PrtDevModeStr
End Type

Private Function GetPrinterInfoEX(ByVal strSrc As String) As
udtAccPrinterDEV
On Error GoTo Proc_Error
'PURPOSE: Get DevNames & DevMode for access report
'strSrc = name of report with the printer settings.
DoCmd.OpenReport strSrc, acDesign 'Open color settings report
With GetPrinterInfoEX
.DevName.RGB = Reports(strSrc).PrtDevNames 'Get DevNames Structure
.DevMode.RGB = Reports(strSrc).PrtDevMode 'Get DevNames Structure
.Source = strSrc
End With
DoCmd.Close acReport, strSrc, acSaveNo
Proc_Exit:
If IsReportLoaded(strSrc) = True Then DoCmd.Close acReport, strSrc,
acSaveNo
Exit Function
Proc_Error:
MsgBox Err.Description
Resume Proc_Exit
End Function

Function IsReportLoaded(ByVal strReport As String) As Integer
' Returns True if the specified report is loaded.
IsReportLoaded = CBool(SysCmd(acSysCmdGetObjectState, acReport,
strReport) <> 0)
End Function

Function Print2AnyPrinter(strReport As String, strFilter As String,
strPrinterSettings As String) As Long
On Error GoTo Proc_Error
'Returns: 0 = Success!
'strReport = Report you want to print
'strFilter = Your Filter If you do not have one then pass "" as the value
'strPrinterSettings = The Report that has the printer settings! (Create a
new report and Set its Default Printer.)
'ISSUES: The reports MUST be opened in Design View on order to set the
PrtDevNames!

Dim udtPrinterDEV As udtAccPrinterDEV
Dim lngRetVal As Long

udtPrinterDEV = GetPrinterInfoEX(strPrinterSettings)

DoCmd.OpenReport strReport, acViewDesign
Reports(strReport).PrtDevNames = udtPrinterDEV.DevName.RGB
If Len(strFilter & "") > 0 Then
Reports(strReport).FilterOn = True
Reports(strReport).Filter = strFilter
End If
DoCmd.OpenReport strReport, acViewPreview
DoCmd.PrintOut acPrintAll 'Print Report
Proc_Exit:
If IsReportLoaded(strReport) = True Then DoCmd.Close acReport,
strReport, acSaveNo
Exit Function
Proc_Error:
MsgBox Err.Description
Print2AnyPrinter = Err.Number
Resume Proc_Exit
End Function
'-------------------------------END-------------------------------------

HTH

Jose
 
A

Ant. G

Jose Hernandez said:
In Access 2002 use the Printer Object
OR
Access97 (Also works in Access 2002) use PrtDevNames

On my access97 can´t find PrtDevNames, I need any library?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top