Wednesday, November 2, 2011

Selecting Text

Once you understand text ranges, selecting text is easy. The following script selects the first paragraph in the active document.

Setting an insertion point is also easy; just make the beginning and ending text locations the same. (In other words, an insertion point is a text range and not a text location.)

Note the use of the constant FV_OBJ_END_OFFSET to determine the end of the paragraph. It includes all contents and the end of paragraph marker.

//get the first paragraph in the active document
var doc = app.ActiveDoc;
var mainflow = doc.MainFlowInDoc;
var tframe = mainflow.FirstTextFrameInFlow;
var pgf = tframe.FirstPgf;

var tRange= new TextRange(); //create a text range
//set the range to be the first paragraph
tRange.beg.obj = pgf;
tRange.beg.offset = 0;
tRange.end.obj = pgf;
tRange.end.offset = Constants.FV_OBJ_END_OFFSET;
//make the selection
doc.TextSelection = tRange;

Here is a paragraph (with a quote from Lewis Carroll), selected using the script.

No comments:

Post a Comment