SearchOptions using Sequence number of the Word - MS Office Word Addin OfficeJS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



SearchOptions using Sequence number of the Word - MS Office Word Addin OfficeJS



Right now I am locating the words in a Word document by searching the term using


context.document.body.search(dict, ignorePunct: true, matchCase: true, matchWholeWord: true );`



But I want to locate a word by its ordinal position in the Word document. I can't find the exact nth word.


nth



Is it possible to do that?





Your question isn't quite clear... Do you mean you want, for example, the fourth instance of the search term?
– Cindy Meister
Aug 10 at 14:27





Example "I have a word in my document" here 2nd word is "have", So using that 2 I need to locate the word and then need to work on the word like highlighting, selection, etc @CindyMeister
– spidie_sridhar
Aug 10 at 14:32


2nd


2





Has your question been answered?
– Cindy Meister
Aug 26 at 11:55





I am in middle of something, need to check @CindyMeister
– spidie_sridhar
Aug 28 at 9:33




2 Answers
2



Speedy: You can use the range.split() method for this. (i.e. this applies to searchResult, body, paragraph, content control, etc.) Assuming that you want to get the 4th word in the paragraph this is how you do it:




async function run()
await Word.run(async (context) =>
//the method returns an array with the split ranges...
// the first parameter is an array of the delimiters you want to use, in this case the space. you can add more if needed.
let myWords = context.document.body.paragraphs.getFirst().split([" "], true, true);
myWords.load();
await context.sync();
if (myWords.items.length >= 3) // just because i want the 4th word :)
console.log(myWords.items[3].text);

);



and if you want to play with it in Script lab here is the gist you can import.



The Office JS APIs don't (yet) have an equivalent to the Word COM object Words that lets you specify a "word" in a given range via an index value. Considering the fact that words are separated by a space you can get the entire text of a document, then split it to an array of "words".


Words


await Word.run(async (context) =>
let ordinalWord = 2;
let rng = context.document.body;
rng.load("text");
await context.sync();
let sContent = rng.text;
let words = sContent.split(" ", ordinalWord);
console.log(words[ordinalWord - 1]);
);



Once you know what that nth word is, you can use the code you already have to search/find the range in the document.


nth



If the word could occur more than once then you may need to expand how the "split" is used in order to get the surrounding words so that the search can be more accurate.





we have some sort of range.word Cindy, check my answer below.
– Juan Balmori
Aug 10 at 17:46





@JuanBalmori Ah, took me a moment to see the difference - it can be used on the range, not just on text. That's helpful :-)
– Cindy Meister
Aug 10 at 18:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard