bscarano

Array Search and _PrintMGR_PrinterExists

12 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thank You Water!

Is there a way to run for each match found?

The machine(s) may have multiple instances of the printers running.  (Copy 1) (Copy 2) etc.

Is there a way to say "for each match. . run this code"

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Most likely, (Copy 1), (Copy 2)

Although, there could be Dymo 330 and a Dymo 330-USB

Edited by bscarano
added additional info

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

#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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hmm, ok, when I try to run, it states (Variable cannot be accessed in this manner)?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#10 ·  Posted

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.

Share this post


Link to post
Share on other sites

#12 ·  Posted

I did not even realize you wrote it.  It is awesome!!

 

Thanks for creating!

Share this post


Link to post
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