Document object (JavaScript API for Office)

Office Add-ins

An abstract class that represents the document the add-in is interacting with.

Last modified: February 12, 2015

Note Note

The name "apps for Office" is changing to "Office Add-ins". During the transition, the documentation and the UI of some Office host applications and Visual Studio tools might still use the term "apps for Office". For details, see New name for apps for Office and SharePoint.

Hosts:

Access, Excel, PowerPoint, Project, Word

Added in

1.0

Last changed in

1.1

See all support details

Office.context.document

Properties

Name

Description

Support notes

bindings

Gets an object that provides access to the bindings defined in the document.

In 1.1, added support for content apps for Access.

customXmlParts

Gets an object that represents the custom XML parts in the document.

mode

Gets the mode the document is in.

In 1.1, added support for content apps for Access.

settings

Gets an object that represents the saved custom settings of the content or task pane app for the current document.

In 1.1, added support for content apps for Access.

url

Gets the URL of the document that the host application currently has open.

In 1.1, added support for content apps for Access.

Methods

Name

Description

Support notes

addHandlerAsync

Adds an event handler for a Document object event.

getActiveViewAsync

Returns the current view of the presentation.

In 1.1, added to support apps for PowerPoint.

getFileAsync

Returns the entire document file in slices of up to 4194304 bytes (4MB).

In 1.1, added support getting file as PDF in apps for PowerPoint and Word.

getFileProperties

Gets file properties of the current document.

In this release, can get only the URL of the document.

In 1.1, added to get the document's URL in apps for Excel, Word, and PowerPoint.

getSelectedDataAsync

Reads the data contained in the current selection of the document.

In 1.1, added support for getting the id, title, and index for the selected range of slides in apps for PowerPoint.

goToByIdAsync

Goes to the specified object or location in the document.

In 1.1, added to support navigation within the document in apps for Excel and PowerPoint.

removeHandlerAsync

Removes an event handler for a Document object event.

setSelectedDataAsync

Writes data to the current selection in the document.

In 1.1, added support for setting formatting on the selected table when writing data in apps for Excel.

Events

Name

Description

Support notes

ActiveViewChanged

Occurs when the user changes the current view of the document.

In 1.1, added to support apps for PowerPoint.

SelectionChanged

Occurs when the selection in the document is changed.

You don't instantiate the Document object directly in your script. To call members of the Document object to interact with the current document or worksheet, use Office.context.document in your script.

The following example uses the getSelectedDataAsync method of the Document object to retrieve the user's current selection as text, and then display it in the app's page.


// Display the user's current selection.
function showSelection() {
    Office.context.document.getSelectedDataAsync(
        "text",                        // coercionType
        {valueFormat: "unformatted",   // valueFormat
        filterType: "all"},            // filterType
        function (result) {            // callback
            var dataValue; 
            dataValue = result.value;
            write('Selected data is: ' + dataValue);
        });
}
// Function that writes to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message; 
}

Support for each API member of the Document object differs across Office host applications. See the "Support details" section of each member's topic for host support information.

For more information about Office host application and server requirements, see Requirements for running apps for Office.

Added in

1.0

Last changed in

1.1

App types

Content, task pane

Library

Office.js

Namespace

Office

Show:
© 2015 Microsoft