Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
RangeFromChild Method
Collapse the table of content
Expand the table of content

TextPattern.RangeFromChild Method

Retrieves a text range enclosing a child element such as an image, hyperlink, Microsoft Excel spreadsheet, or other embedded object. 

Namespace:  System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

'Declaration
Public Function RangeFromChild ( _
	childElement As AutomationElement _
) As TextPatternRange

Parameters

childElement
Type: System.Windows.Automation.AutomationElement

The enclosed object.

Return Value

Type: System.Windows.Automation.Text.TextPatternRange
A range that spans the child element.

ExceptionCondition
ArgumentNullException

The child element is Nothing.

InvalidOperationException

The element is not a child of the text container.

If there is no text in the range where the child element exists, a degenerate (empty) range is returned.

The childElement parameter is either a child of the AutomationElement associated with a TextPattern or from the array of children of a TextPatternRange.

''' ------------------------------------------------------------------- 
''' <summary> 
''' Obtain the text control of interest from the target application. 
''' </summary> 
''' <param name="targetApp"> 
''' The target application. 
''' </param> 
''' <returns> 
''' An AutomationElement. representing a text control. 
''' </returns> 
''' ------------------------------------------------------------------- 
Private Function GetTextElement(ByVal targetApp As AutomationElement) As AutomationElement
    ' The control type we're looking for; in this case 'Document' 
    Dim cond1 As PropertyCondition = _
        New PropertyCondition( _
        AutomationElement.ControlTypeProperty, _
        ControlType.Document)

    ' The control pattern of interest; in this case 'TextPattern'. 
    Dim cond2 As PropertyCondition = _
        New PropertyCondition( _
        AutomationElement.IsTextPatternAvailableProperty, _
        True)

    Dim textCondition As AndCondition = New AndCondition(cond1, cond2)

    Dim targetTextElement As AutomationElement = _
        targetApp.FindFirst(TreeScope.Descendants, textCondition)

    ' If targetText is null then a suitable text control was not found. 
    Return targetTextElement
End Function


...


''' ------------------------------------------------------------------- 
''' <summary> 
''' Obtains a text range spanning an embedded child  
''' of a document control and displays the content of the range. 
''' </summary> 
''' <param name="targetTextElement"> 
''' The AutomationElement. representing a text control. 
''' </param> 
''' ------------------------------------------------------------------- 
Private Sub GetRangeFromChild( _
ByVal targetTextElement As AutomationElement)
    Dim textPattern As TextPattern = _
    DirectCast( _
    targetTextElement.GetCurrentPattern(textPattern.Pattern), _
    TextPattern)

    If (textPattern Is Nothing) Then 
        ' Target control doesn't support TextPattern. 
        Return 
    End If 

    ' Obtain a text range spanning the entire document. 
    Dim textRange As TextPatternRange = textPattern.DocumentRange

    ' Retrieve the embedded objects within the range. 
    Dim embeddedObjects() As AutomationElement = textRange.GetChildren()

    Dim embeddedObject As AutomationElement
    For Each embeddedObject In embeddedObjects
        If (embeddedObject.GetCurrentPropertyValue( _
            AutomationElement.IsTextPatternAvailableProperty) = True) Then 
            ' For full functionality a secondary TextPattern should 
            ' be obtained from the embedded object. 
            ' embeddedObject must be a child of the text provider. 
            Dim embeddedObjectRange As TextPatternRange = _
            textPattern.RangeFromChild(embeddedObject)
            ' GetText(-1) retrieves all text in the range. 
            ' Typically a more limited amount of text would be  
            ' retrieved for performance and security reasons.
            Console.WriteLine(embeddedObjectRange.GetText(-1))
        End If 
    Next 
End Sub

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft