1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| Dim sActivePrinter As String
sActivePrinter = Application.ActivePrinter
Application.ActivePrinter = FindPrinter("PDF Creator")
'traitement
Application.ActivePrinter = sActivePrinter
'Written: November 28, 2009
'Author: Leith Ross
'Summary: Finds a printer by name and returns the printer name and port number.
Function FindPrinter(ByVal PrinterName As String) As String
'This works with Windows 2000 and up
Dim Arr As Variant
Dim Device As Variant
Dim Devices As Variant
Dim Printer As String
Dim RegObj As Object
Dim RegValue As String
Const HKEY_CURRENT_USER = &H80000001
Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
RegObj.enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Devices, Arr
For Each Device In Devices
RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Device, RegValue
If Application.International(xlCountrySetting) = 33 Then
Printer = Device & " sur " & Split(RegValue, ",")(1)
Else
Printer = Device & " on " & Split(RegValue, ",")(1)
End If
If InStr(1, Printer, PrinterName, vbTextCompare) > 0 Then
FindPrinter = Printer
Exit Function
End If
Next
End Function |
Partager