2

Basically what I want is that, I have start and end index now I want to get the Range object from this start and end index.

OR how do I get the start and end index from the existing range object.

Word.run(function (context) {
    var range = context.document.getSelection();
    range.select();

    return context.sync().then(function () {
       console.log('Selected the range.');
    });  
})
.catch(function (error) {

});

Please help me how I can solve this.

Thanks in advance.

Prashant Mhase
  • 111
  • 3
  • 6
  • Please clarify what you mean by "start and end index". The Word.Range object (https://dev.office.com/reference/add-ins/word/range) doesn't seem to have these properties. – Rick Kirkham Jun 02 '17 at 20:42
  • I have a one paragraph and i need to select the particular word from start and end index. – Prashant Mhase Jun 05 '17 at 04:09
  • Sorry. I still don't understand what you mean by "start and end index". – Rick Kirkham Jun 05 '17 at 06:19
  • Suppose I need to select the word "welcome" from the below paragraph. I have the start and end position. Start = 3 and End = 10 **Hi welcome to office js API. Here is the new way to create the addins. Good luck.** Using Word interop we can do like this. [https://msdn.microsoft.com/en-us/library/2a9dt54a.aspx](https://msdn.microsoft.com/en-us/library/2a9dt54a.aspx) Word.Range rng = this.Range(ref start, ref end); rng.Select(); So how I can get the Range object from start and end using the Office js API, – Prashant Mhase Jun 05 '17 at 06:53
  • please check my answer for additional details and samples. your second question can be done. thanks. – Juan Balmori Jun 13 '17 at 23:53

2 Answers2

2

To augment Ricky's answer. Yes we don't support specific range indexes/coordinates as this is super error prone. You can however get the start, end or the entire range of any object. For example you can do something like document.getSelection("start") that will give you a zero-length range of the beginning of the selection (you can also do "end" and if you leave it empty it gets you the whole selection range).

Basically all objects have that capability. You can then use other range operations like expand. Check out this example who gets the sentences from the current insertion point to the end of the paragraph, just to give you an idea on what you can accomplish.

hope this helps. thanks.

function getSentences() {
    Word.run(function (context) {
        // gets the complete sentence  (as range) associated with the insertion point.
        var sentences = context.document
            .getSelection().getTextRanges(["."] /* Using the "." as delimiter */, false /*means without trimming spaces*/);
        context.load(sentences);
        return context.sync()
            .then(function () {
                //  expands the range to the end of the paragraph to get all the complete sentences.
                var sentecesToTheEndOfParagraph = sentences.items[0].getRange()
                    .expandTo(context.document.getSelection().paragraphs
                        .getFirst().getRange("end") /* Expanding the range all the way to the end of the paragraph */).getTextRanges(["."], false);
                context.load(sentecesToTheEndOfParagraph);
                return context.sync()
                    .then(function () {
                        for (var i = 0; i < sentecesToTheEndOfParagraph.items.length; i++) {
                            console.log("Sentence " + (i + 1) + ":"
                                + sentecesToTheEndOfParagraph.items[i].text);
                        }
                    });
            });
    })
        .catch(OfficeHelpers.Utilities.log);
}
Juan Balmori
  • 4,898
  • 1
  • 8
  • 17
  • Hey @juan-balmori - thanks for the example code- that cleared up a few things for me. One thing though- maybe you could add this to the docs? expandTo is not really explained there and an example like this would really help users conceptualise Ranges and expandTo. – Fergie Dec 08 '17 at 05:50
  • thats a great suggestion we will add it there soon. – Juan Balmori Dec 15 '17 at 20:19
1

The office.js Word.Range object doesn't have numeric start and end points like the VSTO Word.Range object. In office.js, to get the range of a particular word in text, you probably need to do one of the following, depending on exactly what your scenario is.

  • Use the Range.getRange(rangeLocation) method.
  • Get the range of the parent paragraph. Then use the Range.getTextRanges(...) method to get all the word ranges in the paragraph and then pick out the range you need from the collection that is returned.
  • Use Range.search or Paragraph.search to search for the word.

See the reference help for Word.Range.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32