This documentation is archived and is not being maintained.

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)

public TextPatternRange RangeFromChild(
	AutomationElement childElement


Type: System.Windows.Automation.AutomationElement

The enclosed object.

Return Value

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


The child element is null.


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 that represents a text provider..
/// </returns>
/// -------------------------------------------------------------------
private AutomationElement GetTextElement(AutomationElement targetApp)
    // The control type we're looking for; in this case 'Document'
    PropertyCondition cond1 =
        new PropertyCondition(

    // The control pattern of interest; in this case 'TextPattern'.
    PropertyCondition cond2 = 
        new PropertyCondition(

    AndCondition textCondition = new AndCondition(cond1, cond2);

    AutomationElement targetTextElement =
        targetApp.FindFirst(TreeScope.Descendants, textCondition);

    // If targetText is null then a suitable text control was not found.
    return targetTextElement;


/// -------------------------------------------------------------------
/// <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 AutomationElment that represents a text control.
/// </param>
/// -------------------------------------------------------------------
private void GetRangeFromChild(AutomationElement targetTextElement)
    TextPattern textPattern =
        as TextPattern;

    if (textPattern == null)
        // Target control doesn't support TextPattern.

    // Obtain a text range spanning the entire document.
    TextPatternRange textRange = textPattern.DocumentRange;

    // Retrieve the embedded objects within the range.
    AutomationElement[] embeddedObjects = textRange.GetChildren();

    // Retrieve and display text value of embedded object.
    foreach (AutomationElement embeddedObject in embeddedObjects)
        if ((bool)embeddedObject.GetCurrentPropertyValue(
           // For full functionality a secondary TextPattern should
           // be obtained from the embedded object.
           // embeddedObject must be a child of the text provider.
            TextPatternRange embeddedObjectRange =
            // GetText(-1) retrieves all text in the range.
            // Typically a more limited amount of text would be 
            // retrieved for performance and security reasons.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0