order: 7 id: word-paragraph-search name: Search description: Shows basic and advanced search capabilities. author: OfficeDev host: WORD api_set: WordApi: '1.1' script: content: | $("#setup").on("click", () => tryCatch(setup)); $("#basic-search").on("click", () => tryCatch(basicSearch)); $("#wildcard-search").on("click", () => tryCatch(wildcardSearch)); async function basicSearch() { // Does a basic text search and highlights matches in the document. await Word.run(async (context) => { const results = context.document.body.search("Online"); results.load("length"); await context.sync(); // Let's traverse the search results and highlight matches. for (let i = 0; i < results.items.length; i++) { results.items[i].font.highlightColor = "yellow"; } await context.sync(); }); } async function wildcardSearch() { // Does a wildcard search and highlights matches in the document. await Word.run(async (context) => { // Construct a wildcard expression and set matchWildcards to true in order to use wildcards. const results = context.document.body.search("$*.[0-9][0-9]", { matchWildcards: true }); results.load("length"); await context.sync(); // Let's traverse the search results and highlight matches. for (let i = 0; i < results.items.length; i++) { results.items[i].font.highlightColor = "red"; results.items[i].font.color = "white"; } await context.sync(); }); } 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 ($10,000.00), 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.paragraphs .getLast() .insertText( "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 Online cover page, header, and sidebar. Click Insert and then choose the Online elements you want from the different Online galleries.", "Replace" ); await context.sync(); }); } 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 basic and advanced search capabilities of the API.

Set up

Try it out

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