Does anyone know how in Word VBA I can check whether a given printer is installed?
Ray
Does anyone know how in Word VBA I can check whether a given printer is installed?
Ray
Hi Ray,
this is a little bit tricky which You have already found out.
I use in different solutions following approach:
Option Explicit
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
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
Declare Function RegEnumKeyEx _
Lib "advapi32.dll" _
Alias "RegEnumKeyExA" _
( _
ByVal hKey As Long, _
ByVal dwIndex As Long, _
ByVal lpName As String, _
lpcbName As Long, ByVal _
lpReserved As Long, _
ByVal lpClass As String, _
lpcbClass As Long, _
lpftLastWriteTime As FILETIME _
) _
As Long
Declare Function RegCloseKey _
Lib "advapi32.dll" _
( _
ByVal hKey As Long _
) _
As Long
Public Function fncEnumInstalledPrintersReg() As Collection
'returns a collection of the currently installed printers as
'it appears in the Windows Registry
'
'variable declarations
Dim tmpFunctionResult As Boolean
Dim aFileTimeStruc As FILETIME
Dim AddressofOpenKey As Long, aPrinterName As String
Dim aPrinterIndex As Integer, aPrinterNameLen As Long
'
'required API constants
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const HKEY_LOCAL_MACHINE = &H80000002
'
'initialise the result of the function to a New (empty) Collection object
Set fncEnumInstalledPrintersReg = New Collection
'initialise other variables
aPrinterIndex = 0
'
'open the Windows Registry key that contains the subkeys with the list
'of currently installed printers
'This key should be located at: "SYSTEM\CURRENTCONTROLSET\CONTROL\PRINT\PRINTERS"
tmpFunctionResult = Not CBool _
( _
RegOpenKeyEx _
( _
hKey:=HKEY_LOCAL_MACHINE, _
lpSubKey:="SYSTEM\CURRENTCONTROLSET\CONTROL\PRINT\PRINTERS", _
ulOptions:=0, _
samDesired:=KEY_ENUMERATE_SUB_KEYS, _
phkResult:=AddressofOpenKey _
) _
)
'
'if we could not open/find the "SYSTEM\CURRENTCONTROLSET\CONTROL\PRINT\PRINTERS"
'Registry key, exit the function; return an empty collection
If tmpFunctionResult = False Then GoTo ExitFunction
'Loop through all the sub-keys in the Registry key we just opened, and read the
'entries for the installed printers
Do
'
'pre-initialise a string to hold the first printer name
aPrinterNameLen = 255 'the length of the string should be
'large enough to hold any printer name
aPrinterName = String(aPrinterNameLen, CStr(0))
'
'read the value of the next registry sub-key
tmpFunctionResult = Not CBool _
( _
RegEnumKeyEx _
( _
hKey:=AddressofOpenKey, _
dwIndex:=aPrinterIndex, _
lpName:=aPrinterName, _
lpcbName:=aPrinterNameLen, _
lpReserved:=0, _
lpClass:=vbNullString, _
lpcbClass:=0, _
lpftLastWriteTime:=aFileTimeStruc _
) _
)
'prepare to read the next sub-key
aPrinterIndex = aPrinterIndex + 1
'
'for as long as there is a sub-key to read the RegEnumKeyEx function
'will return tmpFunctionResult=True. If the result is False, there are
'no more printers and therefore we can exit the loop
If tmpFunctionResult = False Then Exit Do
'
'trim all trailing Null-Strings from the name of the printer
'(the aPrinterNameLen variable has been updated to indicate the
' actual length of the printer name)
aPrinterName = Left(aPrinterName, aPrinterNameLen)
'add the name of the printer that we have just read in the
'fncEnumInstalledPrintersReg collection
On Error Resume Next
fncEnumInstalledPrintersReg.Add aPrinterName
On Error GoTo 0
Loop
'
'close the Windows Registry key that we have opened for reading the collection
'installed printers
Call RegCloseKey(AddressofOpenKey)
'
Exit Function
ExitFunction:
If Not AddressofOpenKey = 0 Then Call RegCloseKey(AddressofOpenKey)
Set fncEnumInstalledPrintersReg = Nothing
End Function
'Here is the Sub that You insert Your code to check.
Sub test()
Dim aPrinter As Variant
Dim stPrinter As String
'The name of the printer You want to check up.
stPrinter = "DeskJet 1600CM"
For Each aPrinter In fncEnumInstalledPrintersReg
If stPrinter = aPrinter Then MsgBox "The Printer is installed."
Next aPrinter
End Sub
Display More
Credit to Stratos Malasiotis for the API-solution.
Kind regards,
Dennis
Hi again
[I was not able to edit the above message?!]
To find out more about the structure of API and how they work following link to Mr Ivan F Moala website may be of interest:
http://www.xcelfiles.com/API_01.html
Kind regards,
Dennis
Thanks, Dennis - what a great response!
Regards.
Ray:):):)
You´re welcome - Glad it worked out for You
Don’t have an account yet? Register yourself now and be a part of our community!