Word JSAPI 1.6 paragraph events not firing?

De Winne, Paul 0 Reputation points
2024-04-22T07:54:25.26+00:00

Hi, I cannot get Word JSAPI Document paragraph events to work. What am I doing wrong?

'use strict';

(function () {

// move global initializations (if any) to Office.initialize

// is called after document is loaded

Office.onReady(function () {

// Office is ready.

$(document).ready(function () {

// The document is ready.

// Use this to check whether the API is supported in the Word client.

if (Office.context.requirements.isSetSupported('WordApi', '1.6')) {

// paragraph event notification

addParagraphEventListeners();

} else {

// Lets you know that this code will not work with your version of Word.

$('#supportedVersion').html('This code requires Word 2021 or later.');

}

});

});

/* =================================================================================================

Paragraph Event Handlers

================================================================================================= */

async function addParagraphEventListeners() {

await Word.run(async (context) => {

context.document.onParagraphAdded.add(paragraphAdded);

context.document.onParagraphChanged.add(paragraphChanged);

context.document.onParagraphDeleted.add(paragraphDeleted);

await context.sync();

console.log("Added paragraph event listeners.");

});

}

async function paragraphAdded(event) {

await Word.run(async (context) => {

console.log(`${event.type} event detected. IDs of paragraphs that were added:`);

console.log(event.uniqueLocalIds);

});

}

async function paragraphChanged(event) {

await Word.run(async (context) => {

console.log(`${event.type} event detected. IDs of paragraphs where content was changed:`);

console.log(event.uniqueLocalIds);

});

}

async function paragraphDeleted(event) {

await Word.run(async (context) => {

console.log(`${event.type} event detected. IDs of paragraphs that were deleted:`);

console.log(event.uniqueLocalIds);

});

}

})();

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
686 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,565 questions
0 comments No comments
{count} votes