Search code examples
ms-wordoffice-jsword-addins

What ending marks should be used to extend a range to the end of the paragraph?


I am coding a word add-in and am not clear how to use the getNextTextRange(endingMarks, trimSpacing) method of the Range class.

Specifically I want to select a new Range starting from the currently selected range and going to the end of the paragraph.

The API for for the method states

endingMarks string[] Required. The punctuation marks and/or other ending marks as an array of strings

That's clear enough if you want to select up to the next comma, period or even space. But what ending marks should you use for a paragraph, a line break, or the end of the document?

I have tried using '\n', '^p' and '¶' but none of these seem to work.

          var nr = selection.getNextTextRange(['¶'],true);
           nr.load("isEmpty,text");
           await context.sync();
           console.log('nr='+nr.text);
         } catch(e) {
          console.log("error, soz");
          console.log(e);
         }

Given a document consisting of one paragraph of text with a blank paragraph after it, and the first word of the paragraph highlighted, this add-in throws a RichApi.Error

We couldn't find the item you requested.

I would expect it to instead print out the remainder of the paragraph.


Solution

  • If I understand your scenario, you can work with the ParagraphCollection.getFirst() method. Please install the Script Lab tool. Open the sample called "Get paragraph from insertion point" for an example.