ITextRange Interface
ITextRange Interface

The ITextRange objects are powerful editing and data-binding tools that allow a program to select text in a story and then examine or change that text.

ITextRange Members

CanEdit The ITextRange::CanEdit method determines if the specified range can be edited.
CanPaste The ITextRange::CanPaste method determines if a data object can be pasted, using a specified format, into the current range.
ChangeCase The ITextRange::ChangeCase method changes the case of letters in this range according to the Type parameter.
Collapse The ITextRange::Collapse method collapses the specified text range into a degenerate point at either the beginning or end of the range.
Copy The ITextRange::Copy method copies the text to a data object.
Cut The ITextRange::Cut method cuts the plain or rich text to a data object or to the Clipboard, depending on the pVar parameter.
Delete The ITextRange::Delete method mimics the DELETE and BACKSPACE keys, with and without the CTRL key depressed.
EndOf The ITextRange::EndOf method moves this range's ends to the end of the last overlapping Unit in the range.
Expand The ITextRange::Expand method expands this range so that any partial units it contains are completely contained.
FindText The ITextRange::FindText method searches up to Count characters for the text given by bstr. The starting position and direction are also specified by Count, and the matching criteria are given by Flags.
FindTextEnd The ITextRange::FindTextEnd method searches up to Count characters for the string, bstr, starting from the range's End cp. The search is subject to the comparison parameter, Flags. If the string is found, the End cp is changed to be the end of the matched string, and pLength is set equal to the length of the string. If the string is not found, the range is unchanged and pLength is set equal to zero.
FindTextStart The ITextRange::FindTextStart method searches up to Count characters for the string, bstr, starting at the range's Start cp (cpFirst). The search is subject to the comparison parameter, Flags. If the string is found, the Start cp is changed to the matched string, and pLength is set equal to the length of the string. If the string is not found, the range is unchanged, and pLength is set equal to zero.
GetChar The ITextRange::GetChar property-get method retrieves the character at the range's Start cp (cpFirst).
GetDuplicate The ITextRange::GetDuplicate property-get method retrieves a duplicate of this range object.
GetEmbeddedObject The ITextRange::GetEmbeddedObject method retrieves a pointer to the embedded object at the start of the specified range, that is, at cpFirst. The range must either be an insertion point or it must select only the embedded object.
GetEnd The ITextRange::GetEnd property-get method retrieves this range's End cp (cpLim).
GetFont The ITextRange::GetFont property-get method retrieves an ITextFont object with the character attributes of the specified range.
GetFormattedText The ITextRange::GetFormattedText property-get method retrieves an ITextRange object with the specified range's formatted text.
GetIndex The ITextRange::GetIndex method retrieves the story index of the Unit parameter at the specified range Start cp (cpFirst). The first Unit in a story has an index value of 1. The index of a Unit is the same for all cps from that immediately preceding the Unit up to the last character in the Unit.
GetPara The ITextRange::GetPara property-get method retrieves an ITextPara object with the paragraph attributes of the specified range.
GetPoint The ITextRange::GetPoint method retrieves screen coordinates for the start or end character position in the text range.
GetStart The ITextRange::GetStart property-get method retrieves the specified range Start cp (cpFirst).
GetStoryLength The ITextRange::GetStoryLength property-get method retrieves the count of characters in the specified range's story.
GetStoryType The ITextRange::GetStoryType property-get method retrieves the type of the specified range's story.
GetText The ITextRange::GetText property-get method retrieves the plain text in this range. The Text property is the default property of the ITextRange interface.
InRange The ITextRange::InRange method determines whether this range is within or at the same text as a specified range.
InStory The ITextRange::InStory method determines whether this range's story is the same as a specified range's story.
IsEqual The ITextRange::IsEqual method determines whether this range has the same character positions and story as those of a specified range.
Move The ITextRange::Move method moves the insertion point forward or backward a specified number of units. If the range is nondegenerate, the range is collapsed to an insertion point at either end, depending on Count, and then is moved.
MoveEnd The ITextRange::MoveEnd method moves the range End a specified number of Units in a specified direction.
MoveEndUntil The ITextRange::MoveEndUntil method moves the range's End to the cp of the first character found that is in the set of characters specified by Cset, provided that the character is found within Count characters of the End.
MoveEndWhile The ITextRange::MoveEndWhile method moves the End either Count characters or just past all contiguous characters that are found in the set of characters specified by Cset, whichever is less.
MoveStart The ITextRange::MoveStart method moves the range Start a specified number of Units in a specified direction.
MoveStartUntil The ITextRange::MoveStartUntil method moves the range's Start to the cp of the first character found that is in the set of characters specified by Cset, provided that character is found within Count characters of Start.
MoveStartWhile The ITextRange::MoveStartWhile method moves the Start either Count characters or just past all contiguous characters that are found in the set of characters specified by Cset, whichever is less.
MoveUntil The ITextRange::MoveUntil method searches up to Count characters for the first character in the set of characters specified by Cset. If a character is found, the range is collapsed to that point. The start of the search and the direction are also specified by Count.
MoveWhile The ITextRange::MoveWhile method starts at a specified end of a range and searches while the characters belong to the set specified by Cset and while the number of characters is less than or equal to Count. The range is collapsed to an insertion point when a non-matching character is found.
Paste The ITextRange::Paste method pastes text from a specified data object. See ITextRange::Copy for further discussion.
ScrollIntoView The ITextRange::ScrollIntoView method scrolls the specified range into view.
Select The ITextRange::Select method sets the Start, End, and story values of the active selection to those of this range.
SetChar The ITextRange::SetChar property-put method sets the character at the range's Start cp.
SetEnd The ITextRange::SetEnd property-put method sets this range's End cp.
SetFont The ITextRange::SetFont property-put method sets this range's character attributes to those given by pFont.
SetFormattedText The ITextRange::SetFormattedText property-put method replaces this range text with formatted text given by pRange.
SetIndex The ITextRange::SetIndex method changes this range to the specified Unit of the story.
SetPara The ITextRange::SetPara property-put method sets the paragraph attributes of this range to those given by pPara.
SetPoint The ITextRange::SetPoint method changes the range based on a specified point at or up through (depending on Extend) the point (x, y) aligned according to Type.
SetRange The ITextRange::SetRange property-put method sets the range endpoints to the specified values.
SetStart The ITextRange::SetStart method sets this range's Start cp
SetText The ITextRange::SetText property-put method replaces the text in this range by the specified text.
StartOf The ITextRange::StartOf method moves the range ends to the start of the first overlapping Unit in the range.

Remarks

Multiple text ranges can be active and work cooperatively on the same story and evolve with the story. For example, if one text range deletes specified text before another text range, the latter tracks the change. In this sense, text ranges are similar to Microsoft Word bookmarks, which also track editing changes. However, bookmarks cannot edit text, while text ranges can. In addition, ranges let you manipulate text without changing the selection or Clipboard, both of which are valuable to end users. The ITextSelection interface inherits from ITextRange and adds some UI-oriented methods and properties as described in the section on ITextSelection.

You can look at a text range using methods based on character positions. Specifically, a text range is characterized by:

  • The first character position, cpFirst, which points at an insertion point immediately preceding the first character (relative to the beginning of the story) in the range.
  • The limit position, cpLim, which points at an insertion point immediately following the last character in the range.

The first character in a story has cpFirst = zero. If a cp argument has a value greater than the number of characters in the story, the number of characters in the story is used instead. If a cp argument is negative, zero is used instead. For those familiar with Microsoft Visual Basic for Applications, call the cpFirst property Start and the cpLim property End (even though the starting position of a range is also an end).

In the following figure, character positions are represented by the lines separating the letters. The corresponding character position values are given beneath the lines. The range starting at cpFirst = 5 and ending at cpLim = 7 contains the two-letter word is. If this figure depicts the complete text in a story, the story length is 30.

Character positions for text string

The length of a range is given by cpLim - cpFirst or equivalently by End - Start. A range with zero length is called a degenerate or empty range and has equal cp* values, that is, cpFirst = cpLim. An example of a degenerate range is the current insertion point. A non-null selection is an example of a nondegenerate range.

Suppose that the range from 5 to 7 indicated by shaded cells in the preceding figure is told to delete its text (see ITextRange::Delete), thereby turning itself into an insertion point. The range from 25 to 29 would automatically track its contents, namely the word text. The following figure would result.

Character positions for text string

In this figure, the range for text now has been automatically adjusted to have cpFirst = 23 and cpLim = 27. The owner of the range does not have to worry about updating the range character position values in the face of editing.

The names of the move methods indicate which end to move, but note that if any method attempts to move one range end past the other, both ends get moved to the target position. As a result, the insertion point is at the target position. The concept is that cpFirst and cpLim always have to obey the fundamental condition

0 <= cpFirst <= cpLim <= # characters in story

or equivalently for a range r, 0 <= r.Start <= r.End <= r.StoryLength, which is what you would expect from the names of these quantities.

Another important feature is that all stories contain an undeletable final CR (0xD) character at the end. So even an empty story has a single character, namely the final CR. A range can select this character, but cannot become an insertion point beyond it. To see how this works, try selecting the final CR in a Word document and then press the RIGHT ARROW key to collapse it. The directory tree will collapse before the final CR, but the CR cannot be deleted. The Text Object Model (TOM) functions the same way. So, if r.Start <= r.End, then r.End <= (r.StoryLength – 1). For a discussion about deleting a CR, see ITextRange::Delete.

Some methods depend on a Unit argument, which can take on the predefined values listed in the following table.

UnitValueMeaning
tomCharacter1Character.
tomWord2Word.
tomSentence3Sentence.
tomParagraph4Paragraph.
tomLine5Line (on display).
tomStory6Story.
tomScreen7Screen (as for PAGE UP/PAGE DOWN).
tomSection8Section.
tomColumn9Table column.
tomRow10Table row.
tomWindow11Upper-left or lower-right of the window.
tomCell12Table cell.
tomCharFormat13Run of constant character formatting.
tomParaFormat14Run of constant paragraph formatting.
tomTable15Table.
tomObject16Embedded object.

Most of the Unit values are self-explanatory. However the following descriptions are provided for additional clarity.

tomWord

The tomWord constant is an end of paragraph or a span of alphanumerics or punctuation including any blanks that follow. To get an on-screen feel for tomWord, watch how the caret moves when you press CTRL+RIGHT ARROW (—>) or CTRL+LEFT ARROW (<—) in a Word document.

tomSentence

The tomSentence constant describes a string of text that ends with a period, question mark, or exclamation mark and is followed either by one or more ASCII white space characters (9 through 0xd and 0x20), or the Unicode paragraph separator (0x2029). The trailing white space is part of the sentence. The last sentence in a story does not need to have a period, question mark, or exclamation mark. The start of a story qualifies as the start of a tomSentence, even if the string there does not qualify as a sentence grammatically. Other sentences must follow a sentence end and cannot begin with a period, question mark, or exclamation mark.

tomParagraph

The tomParagraph constant is a string of text terminated by an end-of-paragraph mark (CRLF, CR, VT (for SHIFT+ENTER), LF, FF, or 0x2029). TOM engines always have an undeletable end-of-paragraph mark at the end of a story. Thus, all TOM stories automatically have at least one tomWord, one tomSentence, and one tomParagraph.

tomLine

The tomLine constant corresponds to one line of text on a display, provided that a display is associated with the range. If no display is associated with a range, tomLine is treated as tomParagraph. A selection automatically has a display and a range that is a duplicate (see ITextRange::GetDuplicate). Other ranges may not have a display, depending on the TOM engine and context.

Methods that move one or both ends in terms of Unit, such as ITextRange::Move, ITextRange::MoveEnd, and ITextRange::MoveStart, depend on the signed Count argument. Except for the ITextSelection geometrical movement commands, if Count is greater than zero, the ends to be moved are moved forward (towards the end of the story), and if Count is less than zero, the ends are moved backward (towards the beginning). The default value of Count for these Move methods is 1. These methods attempt to move Count Units, but movement is never beyond the ends of the story.

Methods that move one or both ends by matching character strings or string patterns, such as ITextRange::MoveWhile, ITextRange::MoveEndWhile, and ITextRange::MoveStartWhile, can move up to a maximum number of characters given by the signed Count argument. If Count is greater than zero, the ends to be moved are moved forward, and if Count is less than zero, the ends are moved backward. Two special Count values, tomForward and tomBackward, are defined. These values are guaranteed to reach the end and the start of the story, respectively. The default value of Count is tomForward.

In Move* methods that turn a nondegenerate range into a degenerate one, such as ITextRange::Move, ITextRange::MoveWhile, and ITextRange::MoveUntil, cpFirst is changed if Count is negative and cpLim is changed if Count is positive. After this movement, the other end of the range is also moved to the new location. See the individual methods for more specific Count information. For nondegenerate ranges, the methods ITextRange::MoveStart, ITextRange::MoveEnd, ITextRange::MoveStartWhile, ITextRange::MoveEndWhile, ITextRange::MoveStartUntil and ITextRange::MoveEndUntil move either the starting position (Start) or the ending position (End).

To select a unit that corresponds to a contiguous range, such as a tomWord, tomSentence, and tomParagraph, use the ITextRange::MoveEnd method. To select a unit that corresponds to a noncontiguous range, such as tomObject, use the ITextRange::EndOf method, since the next object may occur after substantial intermediate text, if at all. To select a tomCell unit, the range must be inside a table.

Examples and further explanation of the Count and Unit arguments follow. Note that TOM engines may not support all of the units in the table above. For example, rich edit controls do not offer the concepts of sections, but rather return E_NOTIMPL when given tomSection. However if a TOM engine does support a unit, it has the index value given in the table.

Applications typically do not implement the ITextRange interface. Microsoft text solutions, such as rich edit controls, implement ITextRange as part of their TOM implementation.

Applications can retrieve an ITextRange pointer by calling the ITextDocument::Range method.

Interface Information

Minimum DLL Versionmsftedit.dll
Inherits from IDispatch
Headertom.h
Minimum operating systems Windows 95 with Rich Edit 2.0, Windows 98, Windows NT 4.0

See Also

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View