FindText Method

TextPatternRange.FindText Method (String, Boolean, Boolean)


Returns a text range subset that contains the specified text.

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

Public Function FindText (
	text As String,
	backward As Boolean,
	ignoreCase As Boolean
) As TextPatternRange


Type: System.String

The text string to search for.

Type: System.Boolean

true if the last occurring text range should be returned instead of the first; otherwise false.

Type: System.Boolean

true if case should be ignored; otherwise false.

Return Value

Type: System.Windows.Automation.Text.TextPatternRange

A text range matching the specified text; otherwise null (Nothing in Microsoft Visual Basic .NET). 

There is no differentiation between hidden and visible text. UI Automation clients can use IsHiddenAttribute to check text visibility.


Use DocumentRange to search the entire document.

Private Function TextFromSelection(ByVal target As AutomationElement) As TextPatternRange
    ' Specify the control type we're looking for, in this case 'Document'
    Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)

    ' target --> The root AutomationElement.
    Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)

    Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)

    If (textpatternPattern Is Nothing) Then
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
        Return Nothing
    End If
    Dim currentSelection As TextPatternRange() = textpatternPattern.GetSelection()
    ' Find 'text' in selection range
    Return currentSelection(0).FindText("text", False, True)
End Function

.NET Framework
Available since 3.0
Return to top
© 2016 Microsoft