0
votes

I'm trying to delete a content control in Word and I'm going through these docs: https://dev.office.com/reference/add-ins/word/contentcontrol

This is my code:

let mycc = context.document.contentControls.getByTag('mycc');
if (mycc) {
    context.load(mycc, 'text');
    context.sync().then(() => {
        mycc.items[0].delete(true);
        context.sync().then(() => { ... })
    })
}

No error is thrown and the code inside the inner most sync works (inserting another content control).

What am I doing wrong?

EDIT: Found the issue. The content control was indeed being deleted, but I wanted to delete the content, as well. Instead of passing true to delete, I should have passed false.

1

1 Answers

3
votes

I'm unable to reproduce the issue that you've described. Given that the code that you've posted does not include any error-handling logic, I'd suspect that perhaps an error is silently being thrown, but you're not detecting it.

The following code snippet successfully deletes the first content control that has the tag mycc, and includes a catch statement that handles any error which might occur inside of the Word.run.

Word.run(function (context) {
    var myContentControls = context.document.contentControls.getByTag("mycc");
    if (myContentControls) {
        context.load(myContentControls, "text");
        return context.sync()
            .then(function () {
                myContentControls.items[0].delete(true);
                return context.sync()
                    .then(function () {
                        console.log("Done!");
                    });
            });
    }
}).catch(OfficeHelpers.Utilities.log);