This forum is closed to new posts and
responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:
I used this code few years ago, so I do not remember exactly how it worked, but you can get and set the default printer with it.
In the form declaration I used this (maybe you do not need everything):
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
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
And I had a button with this sub:
Sub Printer
' 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,"Printer selection","Please select the printer:","",pArray)
If Cstr(PrinterPath)="" Then Exit Sub
WshNetwork.SetDefaultPrinter Cstr(PrinterPath)
Print "Default printer is now " & Cstr(PrinterPath)
app.ActiveWorkbook.PrintOut
WshNetwork.SetDefaultPrinter DefaultPrinter$
Print "Default printer restored to " & DefaultPrinter$
Set WshNetwork = Nothing
Set WshPrinters = Nothing
End Sub
I hope you can use it.
Feedback response number WEBB8NFQ2W created by ~August Fezkisterflar on 11/09/2011