Jump to content

Change text in text block


 Share

Recommended Posts

Hello,

I'm trying to change the text in a TextBlock.  I stumbled onto the solution a couple of days ago but managed to delete the script.  I found an example in Stack Overflow written in C++ and the comments indicated that the proper way to change the text is using the "value" variables.  I've tried several combinations in AutoIt with no success 

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#include "UIA_Constants.au3" ; Can be copied from UIASpy Includes folder

Opt("MustDeclareVars", 1)
HotKeySet('{ESC}', 'Quit')

Example()

Func Example()
    ; Create UI Automation object
    Local $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation8, $sIID_IUIAutomation5, $dtag_IUIAutomation5)
    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_IUIAutomationElement8, $dtag_IUIAutomationElement8)
    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_ClassNamePropertyId, "TextBlock", $pCondition0)
    If Not $pCondition0 Then Return ConsoleWrite("$pCondition0 ERR" & @CRLF)
    ConsoleWrite("$pCondition0 OK" & @CRLF)

    Local $pText1, $oText1
    $oDesktop.FindFirst($TreeScope_Descendants, $pCondition0, $pText1)
    $oText1 = ObjCreateInterface($pText1, $sIID_IUIAutomationElement8, $dtag_IUIAutomationElement8)
    If Not IsObj($oText1) Then Return ConsoleWrite("$oText1 ERR" & @CRLF)
    ConsoleWrite("$oText1 OK" & @CRLF)


    ; --- Control Pattern Properties ---

    ConsoleWrite("--- Control Pattern Properties ---" & @CRLF)

    Local $sLegacyIAccessibleName1
    $oText1.GetCurrentPropertyValue($UIA_LegacyIAccessibleNamePropertyId, $sLegacyIAccessibleName1)
    ConsoleWrite("$sLegacyIAccessibleName1 = " & $sLegacyIAccessibleName1 & @CRLF)


    ; --- LegacyIAccessible Pattern (action) Object ---

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

    Local $pLegacyIAccessiblePattern1, $oLegacyIAccessiblePattern1
    $oText1.GetCurrentPattern($UIA_LegacyIAccessiblePatternId, $pLegacyIAccessiblePattern1)
    $oLegacyIAccessiblePattern1 = ObjCreateInterface($pLegacyIAccessiblePattern1, $sIID_IUIAutomationLegacyIAccessiblePattern, $dtag_IUIAutomationLegacyIAccessiblePattern)
    If Not IsObj($oLegacyIAccessiblePattern1) Then Return ConsoleWrite("$oLegacyIAccessiblePattern1 ERR" & @CRLF)
    ConsoleWrite("$oLegacyIAccessiblePattern1 OK" & @CRLF)


    ; --- LegacyIAccessible Pattern (action) Methods ---

    ConsoleWrite("--- LegacyIAccessible Pattern (action) Methods ---" & @CRLF)
    Local $iStr = "Test String"

    $oLegacyIAccessiblePattern1.SetValue($iStr)
    ConsoleWrite("$oLegacyIAccessiblePattern1.SetValue()" & @CRLF)

    While 1
    WEnd
EndFunc   ;==>Example

Func Quit()
    Exit
EndFunc   ;==>Quit
$oUIAutomation OK
$oDesktop OK
--- Find window/control ---
$pCondition0 OK
$oText1 OK
--- Control Pattern Properties ---
$sLegacyIAccessibleName1 = 12/26/2019 11:38:15 PM
--- LegacyIAccessible Pattern (action) Object ---
$oLegacyIAccessiblePattern1 OK
--- LegacyIAccessible Pattern (action) Methods ---
$oLegacyIAccessiblePattern1.SetValue()

This is the window I'm working with:

1090690254_AutoItForumPost-ChangeTextblockValue.png.414373a2e82bd144bbd81f6d375f182b.png

and here is the UIASpy display

UIASpy.thumb.png.a938ed62f7b529d9e35e26404abbe1be.png

The text in the TextBlock never changes and I've used $oLegacyIAccessiblePattern.CurrentValue($iCurrentValue) on several different attempts (not in script).  The output is always blank.  

Thanks in advance

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

×
×
  • Create New...