Sign in to follow this  
Followers 0
jainajay26

AutoIT-unable to select printer in Print ListView control

3 posts in this topic

Dear All,

I am new to AutoIT tool and need your help. I am trying to automate an application testing using AutoIT. One of the step in testing is to print the report. Once the print window pops up to select printer to print, I am not able to read the print listview control using autoit. It does not read the text from print listview. Can you please help me on how can I select one particular printer in print window using AutoIT.

Thanks a lot!

Share this post


Link to post
Share on other sites



I found the solution, here is the code that worked for me:

Func Main()
    Local Const $dialogTitle = "Print"
    Local Const $timeout = 20
   ; Printer name should be a command line argument.
    Local Const $printerName = "Printer Name"
    Local $windowFound = WinWait($dialogTitle, "", $timeout)
   ; For some reason if the print dialog contains the list view (see below)
   ; and the window did not exist before starting WinWait, then the
   ; window returned seems to be something transient that we can't work
   ; with.  Therefore, try to find the window again.  Unclear why this
   ; would be necessary, but this seems peculiar to this type of print
   ; dialog.
    $windowFound = WinWait($dialogTitle, "", $timeout)
    Local $windowHandle

    If $windowFound Then
       ; Get the last window used by an AutoIt function, namely the window
       ; we were waiting for.
        $windowHandle = WinGetHandle("[LAST]")
    Else
        ConsoleWrite("Could not find window " & $dialogTitle & @CRLF)
        Exit(1)
    EndIf

    Local $listViewHandle = ControlGetHandle($windowHandle, "", "[CLASS:SysListView32; Text:FolderView]")
    If $listViewHandle <> "" Then
       ; ControlListView sometimes doesn't work if this script is compiled as 32-bit
       ; and the application that is printing is 64-bit, or vice versa.
       ; See the documentation for ControlListView.
        ConsoleWrite("Using listview" & @CRLF)
        Local $printerIndex = ControlListView($windowHandle, "", $listViewHandle, "FindItem", $printerName)
        ConsoleWrite("index: " & $printerIndex & @CRLF)
        If $printerIndex < 0 Then
            ConsoleWrite("Could not find printer " & $printerName & " in print list view" & @CRLF)
            Exit(1)
        EndIf
        ControlListView($windowHandle, "", $listViewHandle, "Select", $printerIndex)
       ; Enable the statement below to print.
       ; ControlClick($windowHandle, "", "[CLASS:Button; Text:&Print]")
    Else
       ; We have an old-style print dialog.
       ; Unlike the print dialog with the list view used above, a
       ; 32-bit/64-bit combination of script and application (or vice versa)
       ; is not a problem.
        ConsoleWrite("Using combo" & @CRLF)
        ControlCommand($windowHandle, "", "[CLASS:ComboBox; INSTANCE:1]", "SelectString", $printerName)
       ; Enable the statement below to print.
       ; ControlClick($windowHandle, "", "[CLASS:Button; Text:OK]")
    EndIf

   ; Enable the statement below to cancel printing.
   ; ControlClick($windowHandle, "", "[CLASS:Button; Text:Cancel]")
EndFunc

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
Sign in to follow this  
Followers 0