I am trying to use getAncestorOrNullObject() function to fetch parent section and parent article of selected paragraph in Word document with Microsoft Office Word Add-in in MS Word 365, The issue I'm facing is even if the paragraph's having ancestor still it's returning undefined or null. Here, I've attached sample code and word document i have used, below. So please verify!
async getAncestor() {
Word.run(async (context: any) => {
let range = context.document.getSelection().paragraphs;
context.load(range, 'text,items,font');
await context.sync();
let getAncestorId = range.items[0]?.listItem.getAncestorOrNullObject();
context.load(getAncestorId, 'text');
await context.sync();
context.load(getAncestorId.contentControls, 'items');
await context.sync();
});
}
Any suggestion and solution would be helpful.