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").click(() => tryCatch(setup));
$("#basic-search").click(() => tryCatch(basicSearch));
$("#wildcard-search").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.