Word.Application class
Represents the application object.
- Extends
Remarks
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
create |
Creates a new document by using an optional Base64-encoded .docx file. |
new |
Create a new instance of the |
open |
Opens a document and displays it in a new tab or window. The following are examples for the various supported clients and platforms.
|
retrieve |
Parse styles from template Base64 file and return JSON format of retrieved styles as a string. |
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
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
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();
});
newObject(context)
Create a new instance of the Word.Application
object.
static newObject(context: OfficeExtension.ClientRequestContext): Word.Application;
Parameters
Returns
openDocument(filePath)
Opens a document and displays it in a new tab or window. The following are examples for the various supported clients and platforms.
Remote or cloud location example:
https://microsoft.sharepoint.com/some/path/Document.docx
Local location examples for Windows:
C:\\Users\\Someone\\Documents\\Document.docx
(includes required escaped backslashes),file://mycomputer/myfolder/Document.docx
Local location example for Mac and iOS:
/User/someone/document.docx
openDocument(filePath: string): void;
Parameters
- filePath
-
string
Required. The absolute path of the .docx file. Word on the web only supports remote (cloud) locations, while Word on Windows, on Mac, and on iOS support local and remote locations.
Returns
void
Remarks
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
Required. The template file.
Returns
OfficeExtension.ClientResult<string>
Remarks
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);
});
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(): {
[key: string]: string;
};
Returns
{ [key: string]: string; }
Office Add-ins