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
public TextPatternRange RangeFromChild (
	AutomationElement childElement
public function RangeFromChild (
	childElement : AutomationElement
) : TextPatternRange
Not applicable.



The enclosed object.

Return Value

A range that spans the child element.

Exception typeCondition


The child element is a null reference (Nothing in Visual Basic).


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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0