locked
How to get previous and next paragraph RRS feed

  • Question

  • Hi,

    I'm developing a TaskPane plugin in JS. I'd like to get specified number of previous and next paragraphs from a paragraph containing a cursor. Even if I iterate over the entire document's paragraphs (which I'd like to avoid), the paragraphs don't have any IDs. 

    Could someone point me into the right directions?

    Thank you

    Friday, October 28, 2016 7:06 AM

All replies

  • Hi petrsimon,

    >> the paragraphs don't have any IDs

    We could get ids, but these ids are according to the paragraphs items. The selected paragraph id will not the same as the id in the paragraphs in the body. We could compare the text to find the current id.

    Here is a simple code:

    //get current paragraph
        function current() {
            Word.run(function (context) {
                //get current paragraph
                var r = context.document.getSelection();
                return context.sync().then(
                    function () {
                        var p = r.paragraphs;
                        context.load(p,'text');
                        return context.sync().then(function () {
                            var currentParagraph = p.items[p.items.length - 1];
                            //var currentText = currentParagraph.text;
                            previous(currentParagraph);
                            return context.sync().then(function () {                         
                                   console.log('previous paragraph selected');
                            });
                        });
                    }
                    );            
            });
        }
        function previous(para) {
            Word.run(function (context) {
                //get all paragraphs
                var paragraphs = context.document.body.paragraphs;
                context.load(paragraphs, 'text');
                return context.sync().then(function () {
                    paragraphs.items.forEach(function (element) {
                        //compare text to find the current paragraph
                        if (element.text == para.text)
                        {
                            //get current previous id
                            var previousId = element.m__Id - 1;
                            //select the previous paragraph by id
                            paragraphs.items[previousId].select();
                        }
                    });
                    return context.sync().then(function () {
                        console.log('previous paragraph selected');
                    });
                });
            });
        }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 6:50 AM
  • Thank you Edward. I was afraid that this would be the only way to go :)
    Monday, October 31, 2016 4:57 PM
  • Hi petrsimon,

    Based on the current Office add-in object model, I am afraid there is no direct way to achieve this.

    If this is a new feature you want to include in the future version, I would suggest you submit a feedback.

    http://officespdev.uservoice.com

    If this workaround works for you, I would suggest you mark the reply as answer, and then others could find the solution easily.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 1, 2016 3:21 AM
  • Thank you, for your kind help.
    Thursday, November 3, 2016 12:36 PM
  • Hi petrsimon,

    If your issue has been resolved, I would suggest you mark the solution as answer, then others could find the solution easily.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 1:37 AM