Word.Style class

Represents a style in a Word document.

Extends

Remarks

[ API set: WordApi 1.3 ]

Properties

baseStyle

Specifies the name of an existing style to use as the base formatting of another style.

borders

Specifies a BorderCollection object that represents all the borders for the specified style.

builtIn

Gets whether the specified style is a built-in style.

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

description

Gets the description of the specified style.

font

Gets a font object that represents the character formatting of the specified style.

inUse

Gets whether the specified style is a built-in style that has been modified or applied in the document or a new style that has been created in the document.

linked

Gets whether a style is a linked style that can be used for both paragraph and character formatting.

listTemplate

Gets a ListTemplate object that represents the list formatting for the specified Style object.

nameLocal

Gets the name of a style in the language of the user.

nextParagraphStyle

Specifies the name of the style to be applied automatically to a new paragraph that is inserted after a paragraph formatted with the specified style.

paragraphFormat

Gets a ParagraphFormat object that represents the paragraph settings for the specified style.

priority

Specifies the priority.

quickStyle

Specifies whether the style corresponds to an available quick style.

shading

Gets a Shading object that represents the shading for the specified style. Not applicable to List style.

tableStyle

Gets a TableStyle object representing Style properties that can be applied to a table.

type

Gets the style type.

unhideWhenUsed

Specifies whether the specified style is made visible as a recommended style in the Styles and in the Styles task pane in Microsoft Word after it's used in the document.

visibility

Specifies whether the specified style is visible as a recommended style in the Styles gallery and in the Styles task pane.

Methods

delete()

Deletes the style.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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 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 is passed to it.) Whereas the original Word.Style object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.StyleData) that contains shallow copies of any loaded child properties from the original object.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

Property Details

baseStyle

Specifies the name of an existing style to use as the base formatting of another style.

baseStyle: string;

Property Value

string

Remarks

[ API set: WordApi 1.5 ]

Note: The ability to set baseStyle was introduced in WordApi 1.6.

borders

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Specifies a BorderCollection object that represents all the borders for the specified style.

readonly borders: Word.BorderCollection;

Property Value

Remarks

[ API set: WordApi BETA (PREVIEW ONLY) ]

builtIn

Gets whether the specified style is a built-in style.

readonly builtIn: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

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

description

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Gets the description of the specified style.

readonly description: string;

Property Value

string

Remarks

[ API set: WordApi BETA (PREVIEW ONLY) ]

font

Gets a font object that represents the character formatting of the specified style.

readonly font: Word.Font;

Property Value

Remarks

[ API set: WordApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Updates font properties (e.g., color, size) of the specified style.
await Word.run(async (context) => {
  const styleName = $("#style-name").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to update font properties.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    const font = style.font;
    font.color = "#FF0000";
    font.size = 20;
    console.log(`Successfully updated font properties of the '${styleName}' style.`);
  }
});

inUse

Gets whether the specified style is a built-in style that has been modified or applied in the document or a new style that has been created in the document.

readonly inUse: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

linked

Gets whether a style is a linked style that can be used for both paragraph and character formatting.

readonly linked: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

listTemplate

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Gets a ListTemplate object that represents the list formatting for the specified Style object.

readonly listTemplate: Word.ListTemplate;

Property Value

Remarks

[ API set: WordApi BETA (PREVIEW ONLY) ]

nameLocal

Gets the name of a style in the language of the user.

readonly nameLocal: string;

Property Value

string

Remarks

[ API set: WordApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Applies the specified style to a paragraph.
await Word.run(async (context) => {
  const styleName = $("#style-name-to-use").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to apply.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else if (style.type != Word.StyleType.paragraph) {
    console.log(`The '${styleName}' style isn't a paragraph style.`);
  } else {
    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"
    );
    const paragraph = body.paragraphs.getFirst();
    paragraph.style = style.nameLocal;
    console.log(`'${styleName}' style applied to first paragraph.`);
  }
});

nextParagraphStyle

Specifies the name of the style to be applied automatically to a new paragraph that is inserted after a paragraph formatted with the specified style.

nextParagraphStyle: string;

Property Value

string

Remarks

[ API set: WordApi 1.5 ]

Note: The ability to set nextParagraphStyle was introduced in WordApi 1.6.

paragraphFormat

Gets a ParagraphFormat object that represents the paragraph settings for the specified style.

readonly paragraphFormat: Word.ParagraphFormat;

Property Value

Remarks

[ API set: WordApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Sets certain aspects of the specified style's paragraph format e.g., the left indent size and the alignment.
await Word.run(async (context) => {
  const styleName = $("#style-name").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to update its paragraph format.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    style.paragraphFormat.leftIndent = 30;
    style.paragraphFormat.alignment = Word.Alignment.centered;
    console.log(`Successfully the paragraph format of the '${styleName}' style.`);
  }
});

priority

Specifies the priority.

priority: number;

Property Value

number

Remarks

[ API set: WordApi 1.5 ]

quickStyle

Specifies whether the style corresponds to an available quick style.

quickStyle: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

shading

Gets a Shading object that represents the shading for the specified style. Not applicable to List style.

readonly shading: Word.Shading;

Property Value

Remarks

[ API set: WordApi 1.6 ]

tableStyle

Gets a TableStyle object representing Style properties that can be applied to a table.

readonly tableStyle: Word.TableStyle;

Property Value

Remarks

[ API set: WordApi 1.6 ]

type

Gets the style type.

readonly type: Word.StyleType | "Character" | "List" | "Paragraph" | "Table";

Property Value

Word.StyleType | "Character" | "List" | "Paragraph" | "Table"

Remarks

[ API set: WordApi 1.5 ]

unhideWhenUsed

Specifies whether the specified style is made visible as a recommended style in the Styles and in the Styles task pane in Microsoft Word after it's used in the document.

unhideWhenUsed: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

visibility

Specifies whether the specified style is visible as a recommended style in the Styles gallery and in the Styles task pane.

visibility: boolean;

Property Value

boolean

Remarks

[ API set: WordApi 1.5 ]

Method Details

delete()

Deletes the style.

delete(): void;

Returns

void

Remarks

[ API set: WordApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-styles.yaml

// Deletes the custom style.
await Word.run(async (context) => {
  const styleName = $("#style-name-to-delete").val() as string;
  if (styleName == "") {
    console.warn("Enter a style name to delete.");
    return;
  }

  const style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load();
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    style.delete();
    console.log(`Successfully deleted custom style '${styleName}'.`);
  }
});

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.StyleLoadOptions): Word.Style;

Parameters

options
Word.Interfaces.StyleLoadOptions

Provides options for which properties of the object to load.

Returns

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.Style;

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.Style;

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

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.StyleUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
Word.Interfaces.StyleUpdateData

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.Style): void;

Parameters

properties
Word.Style

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 is passed to it.) Whereas the original Word.Style object is an API object, the toJSON method returns a plain JavaScript object (typed as Word.Interfaces.StyleData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Word.Interfaces.StyleData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created. If this object is part of a collection, you should also track the parent collection.

track(): Word.Style;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

untrack(): Word.Style;

Returns