I'm writing a Word add-in using Office JS. I have created a Word document that has a structure like the following:
<OuterContentControl tag="Outer" cannotDelete>
<InnerContentControl tag="Inner" cannotDelete />
</OuterContentControl>
I'm trying to delete the inner content control with code similar to this:
async function delete() {
await Word.run(async (context) => {
const contentControls = context.document.contentControls.getByTag('Inner').load();
await context.sync();
contentControls.items.forEach(contentControl => {
contentControl.cannotDelete = false;
contentControl.cannotEdit = false;
contentControl.delete(false);
});
await context.sync();
});
}
When .delete
is called against the inner content control i get a GeneralException
, but for the outer content control, it works. Do I need to do something differently to allow the deletion? I've allowed the outer one to be deleted, but that doesn't change anything.
Edit: After attempting Rick's suggestions to go through the "outer" content control's contentControls
property, I'm able to delete some. The ones that can and cannot be deleted are generated the same way. I still get the unhelpful GeneralException
on the ContentControl.delete
call. I've also tried calling .getRange(Word.RangeLocation.whole).delete()
and get the same GeneralException
. It seems to be the last content control that is nested is the one that fails.
Root cause I lock my outer one from deletion. So if I unlock it from deletion, calling delete on the inner works but deletes the outer content control as well. Seems like a bug in Word. Are there workarounds?