word-js-docs

Basic API call (Office 2013)

Performs a basic Word API call using JavaScript with the “common API” syntax (compatible with Office 2013).

This sample executes a code snippet that prints the selected text to the console. Make sure to enter and select text before clicking “Print selection”.

Button: Print selection

function run() {
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text, (asyncResult) => {
        if (asyncResult.status === Office.AsyncResultStatus.Failed) {
            console.error(asyncResult.error.message);
        } else {
            console.log(`The selected data is "${asyncResult.value}".`);
        }
    });
}