Skip to content

4. Find All Occurrences of One Word

Playback Speed:

In this video, you will use a “while” loop to highlight all occurrences of “very” in your document.

By now, your program locates one overused word in a document and highlights it red.

But this word appears more than once in the document.

To highlight all the times “very” appears in your document, use a loop.

In programming, a “loop” runs the same block of code multiple times.

Place a “while” loop around the code that sets the background color.

Above the background color instructions, type “while,” space, and an open parentheses.

Inside the parentheses, add the condition.

The “while loop” should run while the word "very" is found.

“Very” is stored in the variable "searchResult," so type “searchResult” in the parentheses.

This tells the computer: While the word “very” is found, set the background color to red.

Search for “very” again, and restart the loop.

When the word “very” is not found, end the loop.

Next, check if “very” is found in the document.

The while loop will run only if “very” still appears in the document.

If ”very” is not found, the program will end the loop.

Another way to say this is, “the search result is NOT empty.”

If “very” is found, then the search result will contain a value.

If “very” is not found, then the search result is empty.

In Apps Script, an exclamation point means "not," and the word "null" means empty.

Type a space, then an exclamation point, and two equals signs, a space, and the word “null.”

Close the parentheses.

To keep searching for the word “very” after the first instance, type another "findText" function.

Next, insert an open curly brace.

This tells the computer to start the loop.

Add a closed curly brace after the background color code.

This ends the loop.


You can read your code as: “While the search result for "very" is not empty, do the following."

Add comments to your code to remind yourself what each line does.

Within the loop and after the background color code, type “searchResult, space, equals, space, DocumentApp.getActiveDocument.getBody.findText."

Use “very” as the first parameter. Then, use the variable “searchResult” as the second parameter."

Add comments to your code to remind yourself what each line does.


You created a loop that will repeat until the search results are not empty.

Your code already sets the background color to red or another color.

You ended the loop with a curly brace.

Save your code, then test it out!


The code highlights the word "very" in the document every time it appears.

You can easily see how many times you used the word “very,” and can replace some of these with more powerful words.

In the next video, you will add to your program so it searches for other words in the document.

Now, it's your turn: Add a loop that runs continuously while there are still search values to check.

Be sure the "while loop” starts before the background color is set.

Use the "find Text" function to keep searching for “very” after the first time it appears.

Close the "while loop” with a closing curly brace and add comments to your code so you can more easily remember what each step does.

Save and test your code often!

When you’re finished, move on to the next video to find more words in your document.


  1. Add a "while" loop that repeats as long as there ar emore words to search.
  2. Insert the loop before the code that sets the background color.
  3. Find the next occurrence of the word "very."
  4. Close the loop.