Q:

How to select multiple paragraph from selection

I am developing a office js word addin.
Let's say I have three paragraphs in my word document. The user is selecting text from middle of 1st paragraph till middle of 2nd paragraph. When clicking a button in my addin the system should automatically select the text starting from 1st paragraph to till end of 2nd paragraph. The user should select the complete paragraph and not partial paragraphs. If they do the system should automatically select the whole paragraphs.
I need to know whether we have an option to redefine a range in officejs or any other way to achieve my requirement.
Thanks in advance.

A:

interesting question. You can make this work if you use the paragraphs collection of a range, on this case the currently selected range. The paragraphs collection (and in general all collections in the Word js API)  includes the full paragraph, not only what's selected.
Please run this example you will see how the full paragraphs are printed out of a partial selection (as described on your question)

Word.run(function(context) {
        // this will return the full paragraphs within the selection:
        var myParagraphs = context.document.getSelection().paragraphs;
        context.load(myParagraphs);

        return context.sync()

        .then(function(){
            for(var i=0; i< myParagraphs.items.length; i++){
                // here you will get the full paragraphs, not just what the user selected.
                console.log("this is full  paragraph:" + (i + 1) + ":" + myParagraphs.items[i].text);

            }
        })
    });

Here is an example of how can you select the 2 paragraphs using expandTo and select...

Word.run(function (context) {
    var pars = context.document.getSelection().paragraphs;
    pars.load();

    return context.sync().then(function () {
    pars.items[0].getRange().expandTo(pars.items[pars.items.length - 1].getRange()).select(); 
     
      return context.sync()

    }).catch(function (error) {
      console.log(error.message);
    });
})

Hope this sets you in the right path!
-Juan,

