2
votes

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?

1
I don't get an exception from your code, but I do find that contentControls.items.length is 0, so I think that your "Inner" tagged control is just not being seen. I think you have to first get a reference to the "Outer" content control and then call myOuterContentContol.contentControls.getByTag('Inner') to get the inner one. Please try that and if it works I'll make it the answer.Rick Kirkham
@RickKirkham what build are you on? Perhaps that’s changed...Daniel A. White
I'm on Click-to-Run 1806 10214.20000.Rick Kirkham
@RickKirkham I'm on 1708 and can't go higher ATM. can you verify with your contacts at Microsoft that i have to use the inner ranges or not? heres my scriptlab: gist.github.com/daniel-white/0bf1ecbbdc8331d5415ef9d57d32d119Daniel A. White
@RickKirkham i reworked my sample and i had no problems doing the nested. strange how i do get back something in 1708, but i guess its "fixed" in newer builds.Daniel A. White

1 Answers

-1
votes

This does not repro in the latest builds. You can update to 16.0.10211.20004 in your insiders fast.