Declare Function GetTheDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" _
(ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
Function GetDefaultPrinter() As String
' using API above
Dim DefPrinter As String, sLen As Long, hResult As Long
DefPrinter = Space$(255)
sLen = 255
hResult = GetTheDefaultPrinter(ByVal DefPrinter, sLen)
If hResult <> 0 Then GetDefaultPrinter = Left(DefPrinter, sLen - 1)
End Function
Function GetDefaultPrinter2() As String
' Using WMI
Dim scomputer As String
Dim oWMIService, colPrinters, oItem
scomputer = "."
Set oWMIService = GetObject("winmgmts:\\" & scomputer & "\root\cimv2")
Set colPrinters = oWMIService.ExecQuery("Select * from Win32_Printer", , 48)
For Each oItem In colPrinters
If (oItem.Attributes And 4) = 4 Then
GetDefaultPrinter2 = oItem.Name
Exit For
End If
Next
End Function
Function GetDefaultPrinter3() As String
' Using Shell and registry
Set oShell = CreateObject("WScript.Shell")
sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault = ""
On Error Resume Next
sDefault = oShell.RegRead(sRegVal)
sDefault = Left(sDefault, InStr(sDefault, ",") - 1)
On Error GoTo 0
GetDefaultPrinter3 = sDefault
End Function