Edit

Share via


Word.Application class

Represents the Application object.

Extends

Remarks

[ API set: WordApi 1.3 ]

Examples

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

// Updates the text of the current document with the text from another document passed in as a Base64-encoded string.
await Word.run(async (context) => {
  // Use the Base64-encoded string representation of the selected .docx file.
  const externalDoc: Word.DocumentCreated = context.application.createDocument(externalDocument);
  await context.sync();

  if (!Office.context.requirements.isSetSupported("WordApiHiddenDocument", "1.3")) {
    console.warn("The WordApiHiddenDocument 1.3 requirement set isn't supported on this client so can't proceed. Try this action on a platform that supports this requirement set.");
    return;
  }

  const externalDocBody: Word.Body = externalDoc.body;
  externalDocBody.load("text");
  await context.sync();

  // Insert the external document's text at the beginning of the current document's body.
  const externalDocBodyText = externalDocBody.text;
  const currentDocBody: Word.Body = context.document.body;
  currentDocBody.insertText(externalDocBodyText, Word.InsertLocation.start);
  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.

Methods

createDocument(base64File)

Creates a new document by using an optional Base64-encoded .docx file.

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.

newObject(context)

Create a new instance of the Word.Application object.

retrieveStylesFromBase64(base64File)

Parse styles from template Base64 file and return JSON format of retrieved styles as a string.

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

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

Method Details

createDocument(base64File)

Creates a new document by using an optional Base64-encoded .docx file.

createDocument(base64File?: string): Word.DocumentCreated;

Parameters

base64File

string

Optional. The Base64-encoded .docx file. The default value is null.

Returns

Remarks

[ API set: WordApi 1.3 ]

Examples

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

// Updates the text of the current document with the text from another document passed in as a Base64-encoded string.
await Word.run(async (context) => {
  // Use the Base64-encoded string representation of the selected .docx file.
  const externalDoc: Word.DocumentCreated = context.application.createDocument(externalDocument);
  await context.sync();

  if (!Office.context.requirements.isSetSupported("WordApiHiddenDocument", "1.3")) {
    console.warn("The WordApiHiddenDocument 1.3 requirement set isn't supported on this client so can't proceed. Try this action on a platform that supports this requirement set.");
    return;
  }

  const externalDocBody: Word.Body = externalDoc.body;
  externalDocBody.load("text");
  await context.sync();

  // Insert the external document's text at the beginning of the current document's body.
  const externalDocBodyText = externalDocBody.text;
  const currentDocBody: Word.Body = context.document.body;
  currentDocBody.insertText(externalDocBodyText, Word.InsertLocation.start);
  await context.sync();
});

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.ApplicationLoadOptions): Word.Application;

Parameters

options
Word.Interfaces.ApplicationLoadOptions

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

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

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.Application object.

static newObject(context: OfficeExtension.ClientRequestContext): Word.Application;

Parameters

Returns

retrieveStylesFromBase64(base64File)

Parse styles from template Base64 file and return JSON format of retrieved styles as a string.

retrieveStylesFromBase64(base64File: string): OfficeExtension.ClientResult<string>;

Parameters

base64File

string

The template file.

Returns

Remarks

[ API set: WordApi 1.5 ]

Examples

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

// Gets style info from another document passed in as a Base64-encoded string.
await Word.run(async (context) => {
  const retrievedStyles = context.application.retrieveStylesFromBase64(externalDocument);
  await context.sync();

  console.log("Styles from the other document:", retrievedStyles.value);
});

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

Parameters

properties
Word.Interfaces.ApplicationUpdateData

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

Parameters

properties
Word.Application

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

toJSON(): Word.Interfaces.ApplicationData;

Returns