'Open New Project and add Module. Put
one combo box and one button on the form. 'And Put Public
Declaration Below in Module section. 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 'This function is used to set selected printer as default printer 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 'Put These code in Form Section 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
|