Search code examples
javascriptoffice-jsadd-inword-addins

Office js (Word) : Is it possible to get the coordinate (X;Y) of a searched word?


I'm currently on creating a Word addin and I would like to get the position of a searched word because this word can appear several times in the document and I would like to distinguish them according to their position.

Is there a solution or isn't it yet possible ?

I have already searched in the API documentation but couldn't find anything.

To search for a word I use the search() method.

Here is my function:

function find() {
        Word.run(function (context) {
            var searchResult = context.document.body.search('SearchedWord', 
               { ignorePunct: true, matchWholeWord: true });

            searchResult.load('items');

            return context.sync().then(function () {
                console.log('Found count: ' + searchResult.items.length);
                var cpt = 0;
                var id = 0;
                searchResult.items.forEach(function (range) {
                    cpt = cpt + 1;
                    id = 'id'+cpt;
                    const contentControlledItem = range.insertContentControl();
                    contentControlledItem.appearance = "BoundingBox";
                    contentControlledItem.title = 'titleOk'+cpt;
                    setBinding(id, contentControlledItem);
                });

                // Synchronize the document state by executing the queued commands,
                // and return a promise to indicate task completion.
                return context.sync();
            });
        })
    }

Thank you in advance !


Solution

  • This is not possible with the Office JavaScript library. Also, I don't think a word has an X/Y position, because where a word appears depends on such things as the page size, font size, margins, etc.