I have code that finds text in Word and stores the resulting ranges in an array. I am able to navigate the matching result using the array of ranges. I also want to be able to display the surrounding text of a range. This is very similar to how Find works in Word.
For example, I want to show the surrounding text (maybe twenty characters preceding and following) of the range of the second instance of Reading in the following text:
"To change the way a picture fits in your document, click it and a button for layout options appears next to it. When you work on a table, click where you want to add a row or a column, and then click the plus sign. Reading is easier, too, in the new Reading view. You can collapse parts of the document and focus on the text you want. If you need to stop reading before you reach the end, Word remembers where you left off - even on another device."
Here would be the output.
"Reading is easier, too, in the new Reading view. You can collapse parts of the"
I'm using Visual Studio 2017 and Office-js 1.1.
function first() {
ranges[0].select();
ranges[0].font.highlightColor = "aqua";
ranges[0].context.sync();
showSurroundText(ranges[0]);
}
function previous() {
if (index > 0) {
ranges[--index].select();
ranges[index].font.highlightColor = "aqua";
ranges[index+1].font.highlightColor = "yellow";
ranges[index].context.sync();
showSurroundText(ranges[index]);
}
}
function next() {
console.log("Next");
if (index < ranges.length - 1) {
ranges[++index].select();
ranges[index].font.highlightColor = "aqua";
ranges[index-1].font.highlightColor = "yellow";
ranges[index].context.sync();
showSurroundText(ranges[index]);
}
}
function showSurroundText(range:Word.Range){
surroundTextRange = /*need help computing this range */
surroundTextRange.load("text");
$("#output").htmml(surroundTextRange.text);
}
