Jump to content

Toolstrip control works variably


Fiinguer
 Share

Recommended Posts

Hi there,

 

I am STRUGGLING with this and I didn't find an answer.

I have this simple code below, where I need to click a button to export some data from the application "CSDB - Cost Saving Database".

; Create UI Automation object
    Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtag_IUIAutomation )
    If Not IsObj( $oUIAutomation ) Then Return ConsoleWrite( "$oUIAutomation ERR" & @CRLF )
    ConsoleWrite( "$oUIAutomation OK" & @CRLF )

    ; Get Desktop element
    Local $pDesktop, $oDesktop
    $oUIAutomation.GetRootElement( $pDesktop )
    $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
    If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF )
    ConsoleWrite( "$oDesktop OK" & @CRLF )

; --- Find window/control ---

ConsoleWrite( "--- Find window/control ---" & @CRLF )

Local $pCondition0
$oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "CSDB - Cost Saving Database", $pCondition0 )
If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF )
ConsoleWrite( "$pCondition0 OK" & @CRLF )

Local $pWindow1, $oWindow1
$oDesktop.FindFirst( $TreeScope_Children, $pCondition0, $pWindow1 )
$oWindow1 = ObjCreateInterface( $pWindow1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
If Not IsObj( $oWindow1 ) Then Return ConsoleWrite( "$oWindow1 ERR" & @CRLF )
ConsoleWrite( "$oWindow1 OK" & @CRLF )



; --- Find window/control ---

ConsoleWrite( "--- Find window/control ---" & @CRLF )

Local $pCondition1
$oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "toolStripButton2", $pCondition1 )
If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF )
ConsoleWrite( "$pCondition1 OK" & @CRLF )

Local $pButton1, $oButton1
$oWindow1.FindFirst( $TreeScope_Descendants, $pCondition1, $pButton1 )
$oButton1 = ObjCreateInterface( $pButton1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
If Not IsObj( $oButton1 ) Then Return ConsoleWrite( "$oButton1 ERR" & @CRLF )
ConsoleWrite( "$oButton1 OK" & @CRLF )

; --- Invoke Pattern (action) Object ---

ConsoleWrite( "--- Invoke Pattern (action) Object ---" & @CRLF )

Local $pInvokePattern1, $oInvokePattern1
$oButton1.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern1 )
$oInvokePattern1 = ObjCreateInterface( $pInvokePattern1, $sIID_IUIAutomationInvokePattern, $dtag_IUIAutomationInvokePattern )
If Not IsObj( $oInvokePattern1 ) Then Return ConsoleWrite( "$oInvokePattern1 ERR" & @CRLF )
ConsoleWrite( "$oInvokePattern1 OK" & @CRLF )
$oInvokePattern1.invoke()

 

In this first image, I have "Summary" tab selected with some data. When I run the code, it goes to the excel logo button on the toolbar and exports beautifully.

$oUIAutomation OK
$oDesktop OK
--- Find window/control ---
$pCondition0 OK
$oWindow1 OK
--- Find window/control ---
$pCondition1 OK
$oButton1 OK
--- Invoke Pattern (action) Object ---
$oInvokePattern1 OK

 

export_works.thumb.png.2acc4efcd92eb30aab1e6bdeed523cf9.png

 

 

In this second image, I have tab "Prices" selected. And when I run the code I get error:

 

$oUIAutomation OK
$oDesktop OK
--- Find window/control ---
$pCondition0 OK
$oWindow1 OK
--- Find window/control ---
$pCondition1 OK
$oButton1 ERR

 

export_not_works.thumb.png.81f061b99b09228395cfd87224b38a04.png

 

I don't know what else I can do, I have already tried everything I know (kinda new at autoit), but it does not makes any sense to me.

I need to export the data from the tab "Prices", not "Summary".

Anyone who can help me? Please!!

 

(I have uploaded screens of UIASpy showing the main application, info about the toolstrip toolbar and the button. I have already tried first with toolbar and after button, but I get the same situation for the toolbar)

csdb.png

toolstrip.png

button.png

Link to comment
Share on other sites

In such a slightly strange situation, the best thing you can do is print the entire control structure in the window. Print the control structure when the "Summary" tab is selected and print the control structure again when the "Prices" tab is selected. Then you can compare the two control structures and try to find out why the Excel button is only correctly identified in one of the two situations.

You print the entire control structure this way: Right-click the application top window in the tree view in UIASpy and select "Update all childs of element". Right-click the application top window again and select "Create element tree structure".

Then the entire control structure should be shown in the list view. Right-click the list view and select "Copy all items". Now you can Paste the entire control structure into your Editor.

Link to comment
Share on other sites

3 hours ago, LarsJ said:

In such a slightly strange situation, the best thing you can do is print the entire control structure in the window. Print the control structure when the "Summary" tab is selected and print the control structure again when the "Prices" tab is selected. Then you can compare the two control structures and try to find out why the Excel button is only correctly identified in one of the two situations.

You print the entire control structure this way: Right-click the application top window in the tree view in UIASpy and select "Update all childs of element". Right-click the application top window again and select "Create element tree structure".

Then the entire control structure should be shown in the list view. Right-click the list view and select "Copy all items". Now you can Paste the entire control structure into your Editor.

It worked! Thank you very much!

In "Prices" tab it had 2 "toolStrip" toolbars, don't know why.

The only thing it changes between the 2 of them is the native handle property. My question is, can I identify the handle of the "right" toolStrip? Or the handle never changes?

Link to comment
Share on other sites

Good to see that you're able to use UI Automation code.

The toolStrip handle will get a new value every time you open the application. But as long as the application is open, the handle will be the same.

You must divide the search for the Excel button into two steps. First you'll find the correct toolStrip control. Then you'll find the Excel button.

You use

FindAll( $TreeScope_Descendants, $pCondition, $pElements )

to find the two toolStrip controls. The result in $pElements is an UI Automation element array with two elements. Since the control structure of your window is always the same, the index of the array element that contains the correct handle will also always be the same. ConsoleWrite the two handles in the element array and Use UIASpy to identify the correct index. 

Get all texts in a Chrome window shows how to use an UI Automation element array. There's a 7z-file at bottom of post that you can download and run the code.

Once you have identified the correct toolStrip control, you can search for the Excel button with the toolStrip control as the start control.

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...