order: 8 id: word-paragraph-get-word-count name: Get word count description: Counts how many times a word or term appears in the document. author: OfficeDev host: WORD api_set: WordApi: '1.1' script: content: | $("#setup").on("click", () => tryCatch(setup)); $("#count").on("click", () => tryCatch(run)); async function run() { // Counts how many times each term appears in the document. await Word.run(async (context) => { const paragraphs = context.document.body.paragraphs; paragraphs.load("text"); await context.sync(); // Split up the document text using existing spaces as the delimiter. let text = []; paragraphs.items.forEach((item) => { let paragraph = item.text.trim(); if (paragraph) { paragraph.split(" ").forEach((term) => { let currentTerm = term.trim(); if (currentTerm) { text.push(currentTerm); } }); } }); // Determine the list of unique terms. let makeTextDistinct = new Set(text); let distinctText = Array.from(makeTextDistinct); let allSearchResults = []; for (let i = 0; i < distinctText.length; i++) { let results = context.document.body.search(distinctText[i], { matchCase: true, matchWholeWord: true }); results.load("text"); // Map each search term with its results. let correlatedResults = { searchTerm: distinctText[i], hits: results }; allSearchResults.push(correlatedResults); } await context.sync(); // Display the count for each search term. allSearchResults.forEach((result) => { let length = result.hits.items.length; console.log("Search term: " + result.searchTerm + " => Count: " + length); }); }); } async function setup() { await Word.run(async (context) => { const body = context.document.body; body.clear(); body.insertParagraph( "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.", "Start" ); body.insertParagraph( "To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.", "End" ); }); } // Default helper for invoking an action and handling errors. async function tryCatch(callback) { try { await callback(); } catch (error) { // Note: In a production add-in, you'd want to notify the user through your add-in's UI. console.error(error); } } language: typescript template: content: |-
This sample demonstrates how to get the count for words and terms in the document body.

Set up

Try it out

Get the word/term count.

language: html style: content: |- section.samples { margin-top: 20px; } section.samples .ms-Button, section.setup .ms-Button { display: block; margin-bottom: 5px; margin-left: 20px; min-width: 80px; } language: css libraries: | https://appsforoffice.microsoft.com/lib/1/hosted/office.js @types/office-js office-ui-fabric-js@1.4.0/dist/css/fabric.min.css office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css core-js@2.4.1/client/core.min.js @types/core-js jquery@3.1.1 @types/jquery@3.3.1