'arnelgp
'taken from somewhere, i don't recall, sorry
'credit to the owner
Public Function getDefaultPrinter() As String
Dim computer As String
Dim wmiService As Object
Dim installedPrinters As Variant
Dim printer As Object
Dim i As Integer
On Error Resume Next
'Set the computer. Dot means the computer running the code.
computer = "."
'Get the WMI object
Set wmiService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
'Retrieve information about the installed printers (by running a query).
Set installedPrinters = wmiService.ExecQuery("Select * from Win32_Printer")
'If an error occurs in the previous step, inform the user.
If Err.Number <> 0 Then
MsgBox "Could not retrieve the printer information from WMI object!", vbCritical, "WMI Object Error"
Exit Function
End If
'https://stackoverflow.com/questions/41026649/using-managementobjectsearcher-and-win32-printer-to-check-status
'Printer status
'Other (1)
'Unknown (2)
'Idle (3)
'Printing (4)
'Warmup (5)
'Stopped Printing(6)
'Offline (7)
'Loop through all the installed printers and get their name. Check if one of them is the default one.
For Each printer In installedPrinters
'Write the results to the worksheet.
'Debug.Print printer.NAME, printer.PrinterStatus, printer.Local
If (printer.Default) Then
getDefaultPrinter = printer.NAME
'Exit For
End If
Next printer
End Function