Is there a way to change my default printer using vb? I have some reports I want to print to a certain printer and some to a different one. I have a routine that loops through all of the reports to print them so I would like to be able to change the default printer, print the odd reports and then change it back to the default printer after.
I am running Access 2000.
8 19070
try the following code from the tips page of www.aadconsulting.com
Tip and Code Sample: Switching Printers from within your MS Access Application
Fellow Access develepor, Mark Plumpton, of customdata.co.nz, has kindly provided sample code for easily switching printers on the fly while printing Access reports, with code as simple as this... -
SaveDefaultPrinter
-
DefaultPrinter = "HP Laserjet (A3)"
-
DoCmd.OpenReport "rptTest", acViewNormal
-
RestoreDefaultPrinter
Download these demo Access97/2000 databases, which include a class module that performs the function of listing and switching default printers. http://www.aadconsulting.com/printers.zip
The code is also an excellent example of how you can use classes in your MS Access applications.
Hi,
tried your coded class module from printer.zip using ms access 2000. Problem I found is the code doesn't switch printers at all.
When I selected Printer 1 (current default printer) from the list and printed a test page it printed to the default printer (Printer 1) with text saying 'The default printer is: Printer 1)
When I selected Printer 2 from the list of printers (not default) and printed the test page the print job still printed to Printer 1 but the text on the report said the default Printer is Printer 2.
Thus from my observations the code is not switching the default printer from Printer 1 to Printer 2 and then back again.
Is is there a code error or something i'm doing wrong?
Post the class module and the code you used to call the class module, so that I can take a look at it.
Class Module as Below:
'================================================= ==================================================
' Source Module : cdsPrinters
'================================================= ==================================================
' Date Created : 21/03/2005
' Author : Mark Plumpton
' Copyright : ©Custom Data Solutions Ltd, 2005. All rights reserved.
' You may use this code in any project so long as you retain this notice.
' Please send any comments to code@customdata.co.nz
' I didn't figure out how to decipher the API calls, but the rest of code is mine.
'================================================= ==================================================
'
'================================================= ==================================================
' Revision History:
'---------------------------------------------------------------------------------------------------
' Date | Programmer | Comments |
'---------------------------------------------------------------------------------------------------
'
'================================================= ==================================================
'
Option Explicit
Option Compare Database
Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal strReturnedString As String, ByVal nSize As Long) As Long
Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal strReturnedString As String, ByVal nSize As Long) As Long
Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private mstrDefaultPrinter_User As String
Public Function RestoreDefaultPrinter()
DefaultPrinter = mstrDefaultPrinter_User
mstrDefaultPrinter_User = ""
End Function
Public Function SaveDefaultPrinter()
If mstrDefaultPrinter_User = "" Then
mstrDefaultPrinter_User = DefaultPrinter
End If
End Function
Public Property Let DefaultPrinter(strPrinterName As String)
Dim strReturn As String
Dim strPrinterFull As String
'Check for empty string or Null
If strPrinterName <> "" Then
strReturn = String(1000, " ")
GetProfileSection "Devices", strReturn, Len(strReturn)
strReturn = Trim(strReturn)
If InStr(1, strReturn, strPrinterName & "=") > 0 Then
strPrinterFull = GetField(strReturn, strPrinterName & "=", 2)
strPrinterFull = GetField(strPrinterFull, Chr(0), 1)
strPrinterFull = strPrinterName & "," & strPrinterFull
'Set the default printer in the win.ini file, application name = [windows], key name = device
WriteProfileString "windows", "device", strPrinterFull
Else
Err.Raise 1000, , "Printer does not exist in win.ini: " & strPrinterName
End If
End If
End Property
Public Property Get DefaultPrinter() As String
Dim strReturn As String
On Error GoTo ErrHere
strReturn = String(100, " ")
GetProfileString "windows", "device", "", strReturn, Len(strReturn)
If InStr(1, strReturn, ",") > 1 Then
strReturn = Left(strReturn, InStr(1, strReturn, ",") - 1)
Else
strReturn = ""
End If
DefaultPrinter = strReturn
ExitHere:
Exit Property
ErrHere:
MsgBox "Error " & Err & ": " & Err.Description
Resume ExitHere
Resume
End Property
Public Function PrinterList() As String
Dim strReturn As String
Dim i As Long
Dim c As String
Dim strPrinterName As String
Dim strCodePort As String 'this only here for completion, not used
Dim intPart As Integer
Dim strTemp As String
On Error GoTo ErrHere
'Initialise the return string
strReturn = String(1000, " ")
'Get the text of Devices section from the win.ini file
GetProfileSection "Devices", strReturn, Len(strReturn)
strReturn = Trim(strReturn)
'Parse the printer devices
'Part 1 is the printer name, up to the '=' character
'Part 2 is the printer code and port, upto the ':' character
intPart = 1
For i = 1 To Len(strReturn)
c = Mid(strReturn, i, 1)
Select Case Asc(c)
Case Asc("=")
intPart = 2
Case 0 'Null character
'Once the delimiter has been found the device has been parsed
strTemp = strTemp & strPrinterName & ";"
strPrinterName = ""
strCodePort = ""
intPart = 1
Case Else
Select Case intPart
Case 1 'Build the printer name
strPrinterName = strPrinterName & c
Case 2 'Build the Code and Port description
'this only here for completion, not used
strCodePort = strCodePort & c
End Select
End Select
Next i
strTemp = Left(strTemp, Len(strTemp) - 1)
PrinterList = strTemp
ExitHere:
On Error Resume Next
Exit Function
ErrHere:
MsgBox "Error " & Err & ": " & Err.Description
Resume ExitHere
Resume
End Function
Private Function GetField(ByVal strRecord As String, ByVal strDelimiter As String, ByVal intField As Integer) As String
On Error GoTo ExitHere
Dim blnFoundField As Boolean
Dim intCurrentField As Integer
Dim intPos As Integer
Dim intFinish As Integer
If strRecord <> "" Then
intCurrentField = 1
intPos = 1
blnFoundField = False
While Not blnFoundField
If intCurrentField = intField Then
blnFoundField = True
Else
intPos = Nz(InStr(intPos, strRecord, strDelimiter), 0) + Len(strDelimiter)
Select Case intPos
Case Len(strDelimiter)
blnFoundField = True
Case Else
intCurrentField = intCurrentField + 1
End Select
End If
Wend
If intCurrentField = intField Then
intFinish = InStr(intPos, strRecord, strDelimiter)
If intFinish = 0 Then
intFinish = Len(strRecord) + Len(strDelimiter)
Else
intFinish = intFinish
End If
GetField = Mid(strRecord, intPos, intFinish - intPos)
End If
End If
ExitHere:
End Function
Code that called the module:
Dim strReportName As String
Dim strCriteria As String
SaveDefaultPrinter
DefaultPrinter = lstPrinter
strReportName = "rptConceptPrint"
strCriteria = "[NPI_NUMBER]=" & Me![NPI_NUMBER]
DoCmd.OpenReport strReportName, acViewNormal, , strCriteria
RestoreDefaultPrinter
DoCmd.Close
Forgive me for over simplifying, but why not assign each Report its own Default Printer in its Open() Event, then Reset it to the desired Default Printer in the Close() Event of the Report, as in: - Private Sub Report_Open(Cancel As Integer)
-
Dim prtDefault As Printer
-
-
Set Application.Printer = Application.Printers("Microsoft Office Document Image Writer")
-
-
Set prtDefault = Application.Printer
-
End Sub
- Private Sub Report_Close()
-
Call fResetDefaultPrinter
-
End Sub
- Public Function fResetDefaultPrinter()
-
Dim prtDefault As Printer
-
-
Set Application.Printer = Application.Printers("<Default Printer Name>")
-
-
Set prtDefault = Application.Printer
-
End Function
I have all the latest updates and my printer name is correct any other ideas?
Ok I got part of it. When I change it to a local printer it works but when I cahnge to a network printer it does not. Any ideas why?
@puppydoggybuddy Thanks a lot for the great code. It worked perfectly. I had been spinning on this for at least an hour as I had found many examples similar to what @ADezii had posted... but none of them seemed to work for Windows 10.
Your given example smoothly set the new printer and then defaulted back to the original when done. thanks again!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Pablo |
last post by:
Friends,
how can i get the name of the default printer??. I'm
working with winforms. VB,C#, nevermind.
Thanks a lot
Pablo
|
by: bla |
last post by:
Hello all,
Does anybody have working sample VBA code to change printerdriver in a
report and to get a list of available drivers and there functionality?
It is for Access 2000. This version of...
|
by: mathilda |
last post by:
I am trying to send a report to a specific, non-default printer in
code, bypassing the preview mode, but I cannot find the code to do so
anywhere. Can anybody help?
|
by: Willem |
last post by:
Hi,
I'm using System.Drawing.Printing to print some simple
information.
Private WithEvents mDoc As New PrintDocument
The click event on a button has the
mDoc.Print()
|
by: Rick R |
last post by:
We need to change the paper size for the default printer before printing a
PDF file from within a VB.Net application.
printDocument printerSettings is not appropriate as there is no document to...
|
by: rquintana |
last post by:
How can I change by program the default printer in VB.net if I have two
printers connected in one computer?
I want to print diferents reports in diferents printers connected in one
computer and I...
|
by: Bill Nguyen |
last post by:
I'm getting this error message trying to print to the local default printer
(connected using a usb cable):
"Analysis Server: Invlid printer specified"
I have no problem printing to other network...
|
by: Pete Smith |
last post by:
I am using the PrintDocument for my print application.
Currently it is printing to the default printer.
How to change to a different printer?
VB.Net 2003 & .Net Framework 1.1.
Thank you,
|
by: Marc Rowe |
last post by:
Hello,
I have a client who needs to print HTML Files using IE - i have the
automation completed, but cannot find anywhere how to print to a printer that
is not the default printer - the HTML files...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |