| Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (Byval lpAppName As String, Byval lpKeyName As String, Byval lpDefault As String, Byval lpReturnedString As String, Byval nSize As Long) As Long |
| |
| Sub [whatever] |
| Dim WshNetwork As Variant |
| Dim WshPrinters As Variant |
| Dim pArray() As String |
| Dim strBuffer As String * 254 |
| Dim iRetValue As Long |
| Dim DefaultPrinter As String |
| Dim pCount As Integer |
| Dim n As Integer |
| Dim PrinterPath As Variant |
| |
| ' Retreive current default printer information |
| iRetValue = GetProfileString("windows", "device", ",,,", strBuffer, 254) |
| DefaultPrinter$ = Left(strBuffer, Instr(strBuffer, ",") - 1) |
| Print "Current default printer is " & DefaultPrinter$ |
| |
| Set WshNetwork = CreateObject("WScript.Network") |
| Set WshPrinters = WshNetwork.EnumPrinterConnections() |
| |
| 'count the connected printers |
| pCount% = 0 |
| For n = 1 To WshPrinters.Count() Step 2 |
| If wshPrinters(n) <> "" Then pCount%=pCount%+1 |
| Next |
| |
| 'collect the names of the connected printers |
| Redim pArray(pCount%-1) As String |
| pCount% = 0 |
| For n = 1 To WshPrinters.Count() Step 2 |
| If wshPrinters(n) <> "" Then |
| pArray(pCount%) = wshPrinters(n) |
| pCount%=pCount%+1 |
| End If |
| Next |
| |
| PrinterPath = ws.Prompt(4,"Select Printer","Choose a printer from the following list:","",pArray) |
| If Cstr(PrinterPath)="" Then Exit Sub |
| |
| WshNetwork.SetDefaultPrinter Cstr(PrinterPath) |
| |
| Print "Default printer is now " & Cstr(PrinterPath) |
| |
| '***************************************** |
| ' Print what you need to print here |
| '***************************************** |
| |
| WshNetwork.SetDefaultPrinter DefaultPrinter$ |
| Print "Default printer restored to " & DefaultPrinter$ |
| |
| Set WshNetwork = Nothing |
| Set WshPrinters = Nothing |
| End Sub |