Document object (JavaScript API for Office v1.0)

JavaScript API for Office v1.0

An abstract class that represents the document the app is interacting with.

Last modified: June 12, 2014

Office.context.document

Properties

Name

Description

bindings

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

customXmlParts

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

mode

Gets the mode the document is in.

settings

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

url

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

Methods

Name

Description

addHandlerAsync

Adds an event handler for a Document object event.

getFileAsync

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

getSelectedDataAsync

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

removeHandlerAsync

Removes an event handler for a Document object event.

setSelectedDataAsync

Writes data to the current selection in the document.

Events

Name

Description

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; 
}

Supported clients

Excel 2013, Excel 2013 RT, Excel Online, PowerPoint 2013, PowerPoint 2013 RT, Project 2013, Word 2013, Word 2013 RT

Library

Office.js

Namespace

Office

Show:
© 2014 Microsoft