Earlier in the week, I demonstrated that WMI can be use for more than just retrieve information about a PC.
What about configuring different aspects of your PC! That’s right, WMI can do that too.
Configuring a Printer
Below is a simple illustrative example of how we can use WMI to change the ‘Use Printer Offline’ property of any printer. Normally, we need to:
- Type ‘Printer’ in the start menu
- Click on Printers and Scanners
- Click on the Printer of interest
- Click on Open Queue
- Click on Printer
- Click on Use Printer Offline
- Click on the close button of the 2 dialogs
Using the following, we can do avoid that and perform the change with a simple VBA call.
'---------------------------------------------------------------------------------------
' Procedure : WMI_Printer_UseOffline
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Enable/Disable a printer's 'Use Printer Offline' setting
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Early Binding -> Microsoft WMI Scripting VX.X Library
' Late Binding -> None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sPrinterName : Name of the printer to change the configuration of
' bUseOffline : Whether 'Use Printer Offline' is Enable or not
'
' Usage:
' ~~~~~~
' WMI_Printer_UseOffline "HP Smart Tank 5101", True
' Returns -> True/False
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2023-03-06
'---------------------------------------------------------------------------------------
Public Function WMI_Printer_UseOffline(ByVal sPrinterName As String, _
ByVal bUseOffline As Boolean) As Boolean
On Error GoTo Error_Handler
#Const WMI_EarlyBind = False 'True => Early Binding / False => Late Binding
#If WMI_EarlyBind = True Then
Dim oWMI As WbemScripting.SWbemServices
Dim oPrinter As WbemScripting.SWbemObject
#Else
Dim oWMI As Object
Dim oPrinter As Object
#End If
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oPrinter = oWMI.Get("Win32_Printer.DeviceID='" & sPrinterName & "'")
oPrinter.WorkOffline = bUseOffline
oPrinter.Put_
WMI_Printer_UseOffline = True
Error_Handler_Exit:
On Error Resume Next
Set oPrinter = Nothing
Set oWMI = Nothing
Exit Function
Error_Handler:
If Err.Number <> -2147217406 Then 'Printer Not found
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: WMI_Printer_UseOffline" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Function
To Enable ‘Use Printer Offline’, we would simply do:
Call WMI_Printer_UseOffline("HP Smart Tank 5101", True)
To Disable ‘Use Printer Offline’, we would simply do:
Call WMI_Printer_UseOffline("HP Smart Tank 5101", False)
Since WMI has access to just about every aspect of a PC, I hope you are starting to see just how easy it can be used to apply configuration changes with great ease!
The original reason behind this function is that recently (since about December) I’ve had several clients whose printers suddenly stopped working after years of having no issue with them. So I’m assuming some Microsoft update screwed things up again. After some digging this setting appears to resolve the issue. That said, some people found the manual instruction confusing (so many steps with unfamiliar menus), so with this I can simply send them an Excel file that automatically applies the update upon opening.