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

PROGRAMMATICALLY CHANGING THE DEFAULT PRINTER

Status
Not open for further replies.

PrgrmsAll

Programmer
Apr 8, 2003
180
US
I have a non-interactive VB6 application (there is no GUI). I am opening Access behind the scenes and printing a report from it. I want to print to a printer other than the default printer that is setup on the user's PC. I don't want this to be the default printer, just the destination for this particular report.

Is there a way that I can do this programmatically?

Thank you in advance?
 


If you always want to send a particular report to one printer, open the report in design mode in Access, and choose "Page Setup" on the file menu.

Under the "Page" tab, mark the box titled "Use Specific Printer", then click the "Printer..." button to select from the available printers.

When you save your changes, the report will always print to the specified printer.
 
You can use this VB Function



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

Public Declare Function GetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long


Public Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long


Public Function Set_Printer_As_Default(ByVal NewDefault As String) As String

Dim nC As Integer
Dim String_Len As Long
Dim strWinPath As String
Dim strSysFileName As String
Dim strRet As String
Dim Current_Default As String
Dim New_Printer As String
Dim Pr As Printer

' Windows wants "Device Name,Driver Name,Port" in the string to set
' a new default printer.
New_Printer = ""
For Each Pr In Printers
If Pr.DeviceName = NewDefault Then
New_Printer = Pr.DeviceName & "," & Pr.DriverName & "," & Pr.Port
Exit For
End If
Next

' Get the path of the Windows\System directory.
strWinPath = Space$(MAX_FILENAME_LEN)
String_Len = GetWindowsDirectory(strWinPath, MAX_FILENAME_LEN)
strWinPath = Left$(strWinPath, String_Len)
strSysFileName = strWinPath & "\win.ini"

' Get the current default printer
' This will be of the form 'DeviceName,DriverName,Port'
' For example "iDP3210 Full Cut,iDP3210,LPT1:"
strRet = Space$(MAX_FILENAME_LEN)
String_Len = GetPrivateProfileString("windows", "device", "", strRet, MAX_FILENAME_LEN, strSysFileName)
Current_Default = Left$(strRet, String_Len)

' If he didn't provide a new printer name then just return the current default.
If Len(New_Printer) = 0 Then
nC = InStr(1, Current_Default, ",")
If nC = 0 Then nC = Len(Current_Default) + 1
Set_Printer_As_Default = Left$(Current_Default, nC - 1)
Exit Function
End If

' Don't bother to set it if the new default is already the default.
If Current_Default <> New_Printer Then
' Set the selected printer as the default.
String_Len = WritePrivateProfileString(&quot;windows&quot;, &quot;device&quot;, New_Printer, strSysFileName)
SendMessage HWND_BROADCAST, WM_WININICHANGE, 32767&, ByVal &quot;windows&quot;
End If

' Return the name of the previous default printer as the function value.
nC = InStr(1, Current_Default, &quot;,&quot;)
If nC = 0 Then nC = Len(Current_Default) + 1
Set_Printer_As_Default = Left$(Current_Default, nC - 1)

End Function


Just call it with

Dim OldDefault As String
OldDefault = Set_Printer_As_Default NewPrinterName

' ... Do the Printing ...

Call Set_Printer_As_Default(OldDefault)
 
Golom,

That will work fine on NT4 but isn't supported on other platforms. Under W9x/ME use GetPrinter with a PRINTER_INFO5 structure, set the Attributes member to PRINTER_ATTRIBUTE_DEFAULT then call SetPrinter to update the change. On W2000/XP use SetDefaultPrinter (new) which just requires the printer name as a string.

There's an ActiveX DLL, PDXPro, on my web site that amongst other things, gets/sets the default printer. It detects the Windows version and wraps the API calls so you can manage the default printer with a couple of lines of code.



Paul Bent
Northwind IT Systems
 
To Paulbent

Sounds good ... but I beg to differ ... I use it routinely on Win 9x. Like your solution better though.
 
I did that once, looking for a Zebra barcode printer. Didn't bother to change the Windows Default printer, just set the &quot;Printer&quot; object in VB to point to the printer I wanted. Popped-up a MSGBOX if no Zebra printer found.

Public Sub Label_Printer()
Dim X As Printer
If InStr(UCase$(Printer.DeviceName), &quot;ZEBRA&quot;) = 0 Then
If InStr(UCase$(SysDefPrinter.DeviceName), &quot;ZEBRA&quot;) <> 0 Then
Set Printer = SysDefPrinter
Else
For Each X In Printers
If InStr(UCase$(X.DeviceName), &quot;ZEBRA&quot;) > 0 Then
' Set printer as program default.
Set Printer = X
' Stop looking for a printer.
Exit For
End If
Next
End If
End If
If (InStr(UCase$(Printer.DeviceName), &quot;ZEBRA&quot;) = 0) Then Call MsgBox(&quot;Unable to locate a Zebra printer.&quot; + Chr$(13) + Chr$(10) + &quot;Using &quot; + Printer.DeviceName, vbOKOnly, &quot;Cannot find Zebra Printer&quot;)
End Sub

Howard Dingman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top