title: Using Registry to Set Default Printer VBA ONLY When Printer is Active
body: <p>I use Leith Ross' VBA code to cycle through printers on my machine/network, and select the printer that matches the parameters I pass as the default printer. See below:</p>
  
  <pre><code>Function FindPrinter(ByVal PrinterName As String) As String
  
    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 = &amp;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
      Printer = Device &amp; " on " &amp; Split(RegValue, ",")(1)
      If InStr(1, Printer, PrinterName, vbTextCompare) &gt; 0 Then
         FindPrinter = Printer
         Exit Function
       End If
    Next
  
  
  End Function
  </code></pre>
  
  <p>This gets slightly more complicated, because IT sometimes names the printers slightly different names. Still, the function I use finds the printer I am looking for and sets it to the default printer.</p>
  
  <p>The real problem sets when some computers that use this code have duplicate drivers/software of the printers on them, and only one of them is online while the rest are offline. I usually have to get someone from IT to uninstall the offline drivers, or else this function sometimes grabs the offline drivers and the code doesn't work. Instead of having to rely on them to do this, I was interested in modifying this function to only set the active printer to whatever criteria I pass it to ONLY if the printer that matches the <code>Instr</code> criteria in the function is active.</p>
  
  <p>Is there anything in VBA, and/or anything I can add to this code to check if the printer that matches the <code>Instr</code> criteria is actually online, and if it is THEN <code>FindPrinter = Printer</code> <code>Exit Function</code>? My online search results haven't really been helpful; almost everything just either gives this function or some other function to display/set default printer.</p>
  
  <p>EDIT: Just to note, not really looking for a handout answer, although if you provide exactly how to do it then I'd be grateful. If you are more so inclined to link me to documentation on where I can find the answer/solution, I'd be more than happy to do the research.</p>