Set default printer
You will often find yourself in a position where you have designed a report format for one printer and when that report is sent to another printer the layout is messed up.
The following code will allow you to specify the printer that reports are sent to by reseting the systems default printer.
Windows API/Global Declarations
-
Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString 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 Const HWND_BROADCAST = &HFFFF&
-
Public Const WM_WININICHANGE = &H1A
-
Code
-
Public Function SetDefaultPrinter(objPrn As Printer) As Boolean
-
Dim x As Long, sztemp As String
-
sztemp = objPrn.DeviceName & "," & objPrn.DriverName & "," & objPrn.Port
-
x = WriteProfileString("windows", "device", sztemp)
-
x = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0&, "windows")
-
End Function
-
-
Private Sub Command1_Click()
-
Dim x As Printer
-
If MsgBox("Are You Sure Want To Set " & Combo1.Text & " as Default printer ? ", vbYesNo, "Attention") = vbYes Then
-
-
For Each x In Printers
-
If x.DeviceName = Combo1.Text Then
-
SetDefaultPrinter x
-
Exit Sub
-
End If
-
Next
-
-
End If
-
End Sub
-
-
Private Sub Form_Load()
-
Dim x As Printer
-
Dim y As Integer
-
y = 0
-
-
With Combo1 'Scan all available printer and put them
-
For Each x In Printers 'in To combo box.
-
.AddItem x.DeviceName, y
-
y = y + 1
-
Next
-
.ListIndex = 0
-
End With
-
-
End Sub
-