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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print to a specific Printer using VB Code. Anybody?

Status
Not open for further replies.

JARE

Technical User
Jul 27, 2000
50
US
Is there an easy way in VB code to print to a specific
printer? for example.. If a Report that prints out labels
is ran, then instead of printing to default printer the
labels will print to Networked label printer. (Assuming
user has drivers set up on his/her computer.) Or maybe
A way to set another printer as a default and then back again. [sig][/sig]
 
JARE,

The only way I know to do this is to get/set/reset the default printer through the printers collection.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
I created a function in a general module that is used to intercept the printing call my users make. If you use this in your form, attached to the button used to print the report, the user will be prompted to select a printer. If you preview the report, then I make my own tool bar and have this function under the print button.
Here is the form version...> PrintDialog

Here is the tool bar version...> =PrintDialog()

Here is the function

Public Function PrintDialog()
SendKeys &quot;^p&quot;
End Function

Let us know if this works!

John
[sig]<p>John A. Gilman<br><a href=mailto:gms@uslink.net>gms@uslink.net</a><br>[/sig]
 
I don't know any way to send a report to a specific printer from Access, the reports are always sent to the default printer.
But I have a procedure wich permits to change the default printer. Here it is:

Option Explicit

Declare Function GetProfileString Lib &quot;kernel32&quot; _
Alias &quot;GetProfileStringA&quot; _
(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long

Declare Function WriteProfileString Lib &quot;kernel32&quot; _
Alias &quot;WriteProfileStringA&quot; _
(ByVal lpszSection As String, _
ByVal lpszKeyName As String, _
ByVal lpszString As String) As Long

Declare Function SendMessage Lib &quot;user32&quot; _
Alias &quot;SendMessageA&quot; _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lparam As String) As Long

Public Const HWND_BROADCAST = &HFFFF
Public Const WM_WININICHANGE = &H1A

Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Declare Function GetVersionExA Lib &quot;kernel32&quot; _
(lpVersionInformation As OSVERSIONINFO) As Integer

Public Declare Function OpenPrinter Lib &quot;winspool.drv&quot; _
Alias &quot;OpenPrinterA&quot; _
(ByVal pPrinterName As String, _
phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As Long

Public Declare Function SetPrinter Lib &quot;winspool.drv&quot; _
Alias &quot;SetPrinterA&quot; _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Any, _
ByVal Command As Long) As Long

Public Declare Function GetPrinter Lib &quot;winspool.drv&quot; _
Alias &quot;GetPrinterA&quot; _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Any, _
ByVal cbBuf As Long, _
pcbNeeded As Long) As Long

Public Declare Function lstrcpy Lib &quot;kernel32&quot; _
Alias &quot;lstrcpyA&quot; _
(ByVal lpString1 As String, _
ByVal lpString2 As Any) As Long

Public Declare Function ClosePrinter Lib &quot;winspool.drv&quot; _
(ByVal hPrinter As Long) As Long

' constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

' constants for DesiredAccess member of PRINTER_DEFAULTS
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

' constant that goes into PRINTER_INFO_5 Attributes member
' to set it as default
Public Const PRINTER_ATTRIBUTE_DEFAULT = 4

Public Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
dmICMMethod As Long ' // Windows 95 only
dmICMIntent As Long ' // Windows 95 only
dmMediaType As Long ' // Windows 95 only
dmDitherType As Long ' // Windows 95 only
dmReserved1 As Long ' // Windows 95 only
dmReserved2 As Long ' // Windows 95 only
End Type

Public Type PRINTER_INFO_5
pPrinterName As String
pPortName As String
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type

Public Type PRINTER_DEFAULTS
pDatatype As Long
pDevMode As Long
DesiredAccess As Long
End Type

Public Sub SetDefaultPrinter(PrinterName As String)
Dim Buffer As String
Dim DeviceName As String
Dim DriverName As String
Dim PrinterPort As String
Dim r As Long

If Chemin(PrinterName) = &quot;\\&quot; & NomMachine Then
PrinterName = NomFichier(PrinterName)
End If

If Printer.DeviceName = PrinterName Then Exit Sub

Buffer = Space(1024)
r = GetProfileString(&quot;PrinterPorts&quot;, PrinterName, &quot;&quot;, _
Buffer, Len(Buffer))

'Parse the driver name and port name out of the buffer
GetDriverAndPort Buffer, DriverName, PrinterPort

If DriverName <> &quot;&quot; And PrinterPort <> &quot;&quot; Then
Dim DeviceLine As String
Dim l As Long
DeviceLine = PrinterName & &quot;,&quot; & DriverName & &quot;,&quot; & PrinterPort
' Store the new printer information in the [WINDOWS] section of
' the WIN.INI file for the DEVICE= item
r = WriteProfileString(&quot;windows&quot;, &quot;Device&quot;, DeviceLine)
' Cause all applications to reload the INI file:
l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, &quot;windows&quot;)
End If

End Sub


Private Sub GetDriverAndPort(ByVal Buffer As String, DriverName As _
String, PrinterPort As String)

Dim iDriver As Integer
Dim iPort As Integer
DriverName = &quot;&quot;
PrinterPort = &quot;&quot;

'The driver name is first in the string terminated by a comma
iDriver = InStr(Buffer, &quot;,&quot;)
If iDriver > 0 Then

'Strip out the driver name
DriverName = Left(Buffer, iDriver - 1)

'The port name is the second entry after the driver name
'separated by commas.
iPort = InStr(iDriver + 1, Buffer, &quot;,&quot;)

If iPort > 0 Then
'Strip out the port name
PrinterPort = Mid(Buffer, iDriver + 1, _
iPort - iDriver - 1)
End If
End If
End Sub

Let me know if it's usefull!

Sylvie.


 
Jare, I have used the following code. I have not come up with a way to automatically default a different printer but this makes it easier for someone to select a different printer than their current default. I will be glad to send you the database to try this if you would like. Email me at dabloch@twa.com

' Print the object to the selected
' output device.

Dim ctl As Control
Dim varTemp As Variant
Dim varNewDevName As Variant
Dim varOldDevName As Variant
Dim dr As adh_tagDeviceRec
Dim varDM As Variant
Dim varOldDM As Variant
Dim intI As Integer
Dim strName As String
Dim intType As Integer

' This will return the row number from the combo. From this,
' you can get the driver information. This works only
' because cboDestination's Bound Column is 0.
Set ctl = Me!cboDestination

On Error Resume Next
If Len(ctl & &quot;&quot;) = 0 Then
MsgBox &quot;You must specify an output destination!&quot;, vbCritical, &quot;Print Selected Object&quot;
Else
On Error GoTo cmdChosen_ClickErr
If IsNull(Me!lstObjects) Then
Exit Sub
End If
strName = Me!lstObjects
intType = Me!grpListType

' These calls are the &quot;guts&quot; of this routine.
' 1.) Get the old prtDevNames and prtDevMode properties.
' 2.) Create the new prtDevNames string,
' based on the chosen output device.
' 3.) Retrieve the default prtDevMode string for the
' newly selected printer.
' 4.) Copy the current printer settings into the new
' prtDevMode string.
' 5.) Set the new prtDevNames and prtDevMode strings for
' the report/form.
' 6.) Print the thing.
' 7.) Replace the old prtDevNames string (if
' you're not planning on saving your changes,
' this step is unnecessary). Also, replace the original
' prtDevMode string.

' =========================
' 1.) Get the old prtDevNames string from the object. This also
' fills in the varOldDM variable with the old prtDevMode string.
' If your form or report is already open, don't call this. Instead,
' just assign varOldDevName the prtDevNames property, and assign to
' varOldDM the prtDevMode property of the report or form.

varOldDevName = adhRetrieveDevNames(strName, intType, varOldDM)

On Error Resume Next
' 2.) Create the new prtDevNames string.
' Fill in dr with the device, driver and port for the selected printer.
' If you're using this code elsewhere, you probably won't want to call
' adhFillStructure(), but instead you just need to fill in a adh_tagDeviceRec
' structure with the three pieces:
' dr.strDeviceName == the name for the device, as in &quot;HP LaserJet 4/4M&quot;
' dr.strDriverName == the name of the driver, without the .DRV, as in &quot;HPPCL5E&quot;
' dr.strPort == the name of the physical port where it's attached, as in &quot;LPT1:&quot;

dr = adhFillStructure(ctl)

' You'll want to call adhBuildDevNames() to build up the comma-delimited
' string from your DeviceRec. Although you could do this yourself, it's
' a lot easier just to call the routine.
varNewDevName = adhBuildDevNames(dr)
If Err <> 0 Then
MsgBox &quot;Unable to create prtDevNames string.&quot;, vbCritical, &quot;Print Selected Object&quot;
GoTo cmdChosen_ClickExit
End If
On Error GoTo cmdChosen_ClickErr

' 3.) Get the default prtDevMode string for the new printer.
' This step is imperative. You MUST retrieve the default devMode
' structure for the new printer.

varDM = adhGetDefaultDM(dr)
If Len(varDM & &quot;&quot;) = 0 Then
MsgBox &quot;Unable to retrieve default DevMode structure.&quot;, vbCritical, &quot;Print Selected Object&quot;
GoTo cmdChosen_ClickExit
End If

' 4.) Copy the old print setup values to the new print setup.
' You don't really need to do this, and it might actually
' cause you trouble, since your new printer may not
' support the same feature set as your old one. In any
' case, you're best off NOT calling this unless you have control
' over exactly the printers installed.
adhCopyDMValues varOldDM, varDM

' 5.) Set the prtDevNames property with the new string.
' Also set the prtDevMode property.
' Unless you must, you don't need to call this function.
' Just set the prtDevNames and prtDevMode properties of your
' object directly. This sub just exists to generalize that
' for forms or reports.
adhSetDevName strName, intType, varNewDevName, varDM

' 6.) Now print the thing. This function
' leaves the object in design mode.
' Again, this is just meant to generalize the printing
' process so it works for both forms and reports. Don't
' bother using this in your own apps.
DoPrint False

' 7.) Since the object's in design mode, this
' call can replace the prtDevName string.
' Once again, in your own apps, just replace the
' prtDevNames and prtDevMode properties of your object
' with the old values. This function is here to
' generalize for this example only. But there's no point
' doing it if varOldDevName is Null.
If Not IsNull(varOldDevName) Then
adhSetDevName strName, intType, varOldDevName, varOldDM
End If

cmdChosen_ClickExit:
DoClose strName, intType
End If
Exit Sub

cmdChosen_ClickErr:
MsgBox &quot;Error: &quot; & Error & &quot; (&quot; & Err & &quot;)&quot;, vbCritical, &quot;Print Selected Object&quot;
Resume cmdChosen_ClickExit

End Sub
[sig][/sig]
 
I was thumbing through my Access 97 Developer's Handbook and came across a chapter that is almost dedicated to this. We are talking at least 10 pages of explanation, so I don't want to start typing it here.

This is one of those Books/CD's, so I would email a copy of the database to anyone that would like to dig through it. I can also be available for explanation after that. The samples are pretty cool. Everything from printing a sample form to what ever printer is on your local network, to being able to select a network printer and have it tell you EVERYTHING about it (driver info, supported paper sizes, bins, resolution, font support, etc...).

Any one interested, let me know.
[sig]<p>Terry M. Hoey<br><a href=mailto:th3856@txmail.sbc.com>th3856@txmail.sbc.com</a><br><a href= > </a><br>Ever notice that by the time that you realize that you ran a truncate script on the wrong instance, it is too late to stop it?[/sig]
 
After posting this, I noticed that some of code in dabloch's post is from that database that I mentioned. There is much more to it, so if you are interested, let me know. [sig]<p>Terry M. Hoey<br><a href=mailto:th3856@txmail.sbc.com>th3856@txmail.sbc.com</a><br><a href= > </a><br>Ever notice that by the time that you realize that you ran a truncate script on the wrong instance, it is too late to stop it?[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top