Word.SearchOptions class
Specifies the options to be included in a search operation. To learn more about how to use search options in the Word JavaScript APIs, read Use search options to find text in your Word add-in.
- Extends
Remarks
Examples
// Search using a wildcard
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document with a wildcard
// for any string of characters that starts with 'to' and ends with 'n'.
const searchResults = context.document.body.search('to*n', {matchWildcards: true});
// Queue a command to load the search results and get the font property values.
searchResults.load('font');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = 'pink';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
});
Properties
| context | The request context associated with the object. This connects the add-in's process to the Office host application's process. |
| ignore |
If provided, specifies whether to ignore all punctuation characters between words. The default is |
| ignore |
If provided, specifies whether to ignore all whitespace between words. The default is |
| match |
If provided, specifies whether to perform a case sensitive search. The default is |
| match |
If provided, specifies whether to match words that begin with the search string. The default is |
| match |
If provided, specifies whether to match words that end with the search string. The default is |
| match |
If provided, specifies whether to find only entire words, not text that's part of a larger word. The default is |
| match |
If provided, specifies whether the search will be performed using special search operators. The default is |
Methods
| load(options) | Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| load(property |
Queues up a command to load the specified properties of the object. You must call |
| new |
Create a new instance of the |
| set(properties, options) | Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type. |
| set(properties) | Sets multiple properties on the object at the same time, based on an existing loaded object. |
| toJSON() | Overrides the JavaScript |
Property Details
context
The request context associated with the object. This connects the add-in's process to the Office host application's process.
context: RequestContext;
Property Value
ignorePunct
If provided, specifies whether to ignore all punctuation characters between words. The default is false. Corresponds to the _Ignore punctuation characters_ check box in the Find and Replace dialog box.
ignorePunct: boolean;
Property Value
boolean
Remarks
ignoreSpace
If provided, specifies whether to ignore all whitespace between words. The default is false. Corresponds to the _Ignore white-space characters_ check box in the Find and Replace dialog box.
ignoreSpace: boolean;
Property Value
boolean
Remarks
matchCase
If provided, specifies whether to perform a case sensitive search. The default is false. Corresponds to the _Match case_ check box in the Find and Replace dialog box.
matchCase: boolean;
Property Value
boolean
Remarks
matchPrefix
If provided, specifies whether to match words that begin with the search string. The default is false. Corresponds to the _Match prefix_ check box in the Find and Replace dialog box.
matchPrefix: boolean;
Property Value
boolean
Remarks
matchSuffix
If provided, specifies whether to match words that end with the search string. The default is false. Corresponds to the _Match suffix_ check box in the Find and Replace dialog box.
matchSuffix: boolean;
Property Value
boolean
Remarks
matchWholeWord
If provided, specifies whether to find only entire words, not text that's part of a larger word. The default is false. Corresponds to the _Find whole words only_ check box in the Find and Replace dialog box.
matchWholeWord: boolean;
Property Value
boolean
Remarks
matchWildcards
If provided, specifies whether the search will be performed using special search operators. The default is false. Corresponds to the _Use wildcards_ check box in the Find and Replace dialog box.
matchWildcards: boolean;
Property Value
boolean
Remarks
Method Details
load(options)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(options?: Word.Interfaces.SearchOptionsLoadOptions): Word.SearchOptions;
Parameters
Provides options for which properties of the object to load.
Returns
Examples
// Ignore punctuation search
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document and ignore punctuation.
const searchResults = context.document.body.search('video you', {ignorePunct: true});
// Queue a command to load the search results and get the font property values.
searchResults.load('font');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
});
// Search based on a prefix
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document based on a prefix.
const searchResults = context.document.body.search('vid', {matchPrefix: true});
// Queue a command to load the search results and get the font property values.
searchResults.load('font');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = '#FFFF00'; //Yellow
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
});
// Search based on a suffix
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document for any string of characters after 'ly'.
const searchResults = context.document.body.search('ly', {matchSuffix: true});
// Queue a command to load the search results and get the font property values.
searchResults.load('font');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'orange';
searchResults.items[i].font.highlightColor = 'black';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
});
// Search using a wildcard
// Run a batch operation against the Word object model.
await Word.run(async (context) => {
// Queue a command to search the document with a wildcard
// for any string of characters that starts with 'to' and ends with 'n'.
const searchResults = context.document.body.search('to*n', {matchWildcards: true});
// Queue a command to load the search results and get the font property values.
searchResults.load('font');
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
console.log('Found count: ' + searchResults.items.length);
// Queue a set of commands to change the font for each found item.
for (let i = 0; i < searchResults.items.length; i++) {
searchResults.items[i].font.color = 'purple';
searchResults.items[i].font.highlightColor = 'pink';
searchResults.items[i].font.bold = true;
}
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
await context.sync();
});
load(propertyNames)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNames?: string | string[]): Word.SearchOptions;
Parameters
- propertyNames
-
string | string[]
A comma-delimited string or an array of strings that specify the properties to load.
Returns
load(propertyNamesAndPaths)
Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Word.SearchOptions;
Parameters
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.
Returns
newObject(context)
Create a new instance of the Word.SearchOptions object.
static newObject(context: OfficeExtension.ClientRequestContext): Word.SearchOptions;
Parameters
Returns
set(properties, options)
Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.
set(properties: Interfaces.SearchOptionsUpdateData, options?: OfficeExtension.UpdateOptions): void;
Parameters
- properties
- Word.Interfaces.SearchOptionsUpdateData
A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.
- options
- OfficeExtension.UpdateOptions
Provides an option to suppress errors if the properties object tries to set any read-only properties.
Returns
void
set(properties)
Sets multiple properties on the object at the same time, based on an existing loaded object.
set(properties: Word.SearchOptions): void;
Parameters
- properties
- Word.SearchOptions
Returns
void
toJSON()
Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that's passed to it.) Whereas the original Word.SearchOptions object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.SearchOptionsData) that contains shallow copies of any loaded child properties from the original object.
toJSON(): Word.Interfaces.SearchOptionsData;