Word JSAPI 1.6 paragraph events not firing?
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);
});
}
})();