Jump to content

Array Search and _PrintMGR_PrinterExists


bscarano
 Share

Recommended Posts

Hello Everyone,

 

What I am trying to accomplish is have an array with Values (printer names) and check that against the installed printers using _PrintMGR_PrinterExists. 

If the printer exists on the system, delete any printer jobs and then delete the printer.  I have

I'm not sure how to code the array check to the printers installed.

 

#include "PrintMgr.au3"
#include <Array.au3>

$LblPrnArr[2] = ["DYMO LabelWriter 330","DYMO LabelWriter 330-USB"]

For $i = 0 to UBound($LblPrnArr) -1
    _ArrayDisplay($LblPrnArr)
    Local $sSearch = _Printmgr_PrinterExists($LblPrnArr[2])
    If _Printmgr_PrinterExists($LblPrnArr[2]) = 1 Then ;If installed printer matches
        MsgBox(0,"It's here","It's here!")
    Else ; not installed
        MsgBox(0,"Negative", "Your code didn't work")
    EndIf
Next

 

Any help is appreciated.

Link to comment
Share on other sites

Should be something like this:

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr[2] = ["DYMO LabelWriter 330","DYMO LabelWriter 330-USB"]
Global 
For $i = 0 to UBound($LblPrnArr) -1
    If _Printmgr_PrinterExists($LblPrnArr[$i]) = 1 Then ; If installed printer matches
        MsgBox(0,"It's here","Printer " & $LblPrnArr[$i] & " found!")
    Else ; not installed
        MsgBox(0,"Negative", "Your code didn't work")
    EndIf
Next

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

To cancel jobs and remove the printer :

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr[2] = ["DYMO LabelWriter 330","DYMO LabelWriter 330-USB"]

For $i = 0 to UBound($LblPrnArr) -1
    If _Printmgr_PrinterExists($LblPrnArr[$i]) Then ; If installed printer matches
        If _PrintMgr_CancelAllJobs($LblPrnArr[$i]) Then
            _PrintMgr_RemovePrinter($LblPrnArr[$i])
        EndIf
    Else ; not installed
        MsgBox(0,"Negative", "Printer " & $LblPrnArr[$i] & " not found")
    EndIf
Next

 

Link to comment
Share on other sites

Could there be printers with unique names like "DYMO LabelWriter 330 (Copy 1)" and "DYMO LabelWriter 330 (Copy 2)" or the same printer name "DYMO LabelWriter 330" multiple times?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr[2] = ["DYMO LabelWriter 330", "DYMO LabelWriter 330-USB"]
Global $aPrinters = _PrintMgr_EnumPrinter() ; Get a list of all printers

For $i = 0 To $aPrinters[0] ; Loop through all printers
    For $j = 0 To UBound($LblPrnArr) - 1 ; Loop through all printer types to check
        If StringInStr($aPrinters[$i], $LblPrnArr($j)) Then
            MsgBox(0, "It's here", "Printer " & $aPrinters[0] & " matches " & $LblPrnArr[$j] & "!")
        Else ; not installed
            MsgBox(0, "Negative", "Your code didn't work")
        EndIf
    Next
Next

Get a list of all printers and then check for each printer name if it contains (substring) one of the printer types listed in $lblPrnArr.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You can search printers by name using a filter with _PrintMgr_EnumPrinter :

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr = ["DYMO LabelWriter 330*", "HP Deskjet 2540*"]

For $i = 0 To UBound($LblPrnArr) - 1 ; Loop through all printer types to check
    $aPrinters = _PrintMgr_EnumPrinter($LblPrnArr[$i])
    If Not @error And $aPrinters[0] Then
        ConsoleWrite("!" & $aPrinters[0] & " printer(s) matching '" & $LblPrnArr[$i] & "' found : " & @CRLF)
        For $j = 1 To $aPrinters[0]
            ConsoleWrite(" - " & $aPrinters[$j] & @CRLF)
;~          If _PrintMgr_CancelAllJobs($aPrinters[$j]) Then _PrintMgr_RemovePrinter($aPrinters[$j])   ; <--- Cancel jobs and remove printer
        Next
    Else
        ConsoleWrite("!No printer matches '" & $LblPrnArr[$i] & "'" & @CRLF)
    EndIf
Next

Edit : because the printer name is not a sure way, I recommend you to check for the driver name :

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr = ["DYMO LabelWriter 330", "HP Deskjet 2540"]

Local $aPrinters = _PrintMgr_EnumPrinterProperties()
For $i = 0 To UBound($aPrinters) - 1
    $sPrinterName = $aPrinters[$i][53]
    $sDriverName = $aPrinters[$i][31]
    For $j = 0 To UBound($LblPrnArr) - 1
        If StringInStr($sDriverName, $LblPrnArr[$j]) Then
            ConsoleWrite("Then printer " & $sPrinterName & " uses the driver " & $LblPrnArr[$j] & @CRLF)
;~          If _PrintMgr_CancelAllJobs($sPrinterName) Then _PrintMgr_RemovePrinter($sPrinterName])   ; <--- Cancel jobs and remove printer
        EndIf
    Next
Next

 

Edited by jguinch
Link to comment
Share on other sites

34 minutes ago, jguinch said:

You can search printers by name using a filter with _PrintMgr_EnumPrinter :

#include "PrintMgr.au3"
#include <Array.au3>

Global $LblPrnArr = ["DYMO LabelWriter 330*", "HP Deskjet 2540*"]

For $i = 0 To UBound($LblPrnArr) - 1 ; Loop through all printer types to check
    $aPrinters = _PrintMgr_EnumPrinter($LblPrnArr[$i])
    If Not @error And $aPrinters[0] Then
        ConsoleWrite("!" & $aPrinters[0] & " printer(s) matching '" & $LblPrnArr[$i] & "' found : " & @CRLF)
        For $j = 1 To $aPrinters[0]
            ConsoleWrite(" - " & $aPrinters[$j] & @CRLF)
;~          If _PrintMgr_CancelAllJobs($aPrinters[$j]) Then _PrintMgr_RemovePrinter($aPrinters[$j])   ; <--- Cancel jobs and remove printer
        Next
    Else
        ConsoleWrite("!No printer matches '" & $LblPrnArr[$i] & "'" & @CRLF)
    EndIf
Next

 

It worked!  Thank You all so much.  I was racking my brain trying to figure this out.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...