'Open
New Project and add a module with these codes on it. 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 Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value. Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value. Public Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Public Const REG_SZ = 1 ' Unicode nul terminated string Public Const HWND_BROADCAST = &HFFFF& Public Const WM_WININICHANGE = &H1A Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const KEY_WRITE = &H20006 Public Const KEY_READ = &H20019 Public Function SetDefaultPrinter(x As String) Dim hregkey As Long Dim secattr As SECURITY_ATTRIBUTES Dim neworused As Long Dim stringbuffer As String Dim slength As Long Dim retval As Long subkey = "\Config\0001\System\CurrentControlSet\Control\Print\Printers" secattr.nLength = Len(secattr) secattr.lpSecurityDescriptor = 0 secattr.bInheritHandle = True retval = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused) If retval <> 0 Then Debug.Print "Error opening or creating registry key ---aborting" End End If stringbuffer = Trim(x) & vbNullChar retval = RegSetValueEx(hregkey, "Default", 0, REG_SZ, ByVal stringbuffer, Len(stringbuffer)) retval = RegCloseKey(hregkey) x = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0&, "windows") End Function 'Put one combobox and one button on the form. Private Sub Command1_Click() If MsgBox("Are You Sure Want to Set " & Combo1.Text & " as Default printer ? ", vbYesNo, "Attention") = vbYes Then SetDefaultPrinter Combo1.Text End If End Sub Private Sub Form_Load() Dim x As Printer Dim y As Integer y = 0 With Combo1 For Each x In Printers .AddItem x.DeviceName, y y = y + 1 Next .ListIndex = 0 End With End Sub
|