contentControl delete issue in word using office js

39 views Asked by At

I appreciated for contentControl but I am facing a issue when I deleting contentControl this is deleting contentControl only gives error when I was deleting contentControl when contentControl is inside another contentControl for example

1st contentControl

2nd is inside 1st

 `3rd is inside second` 

now second is inside first and deleting successfully and third is inside second but when I am trying to delete third that was gives error generalException = "GeneralException

Error Image

this is my code for deleting contentControl with its tag.

`

async function deleteContentControlByTag(tagToDelete) {
     try {
          await Word.run(async (context) => {
          const contentControls = context.document.contentControls;
          context.load(contentControls, 'items');

          await context.sync();

           // Find the content control with the specified tag
          const contentControl = contentControls.items.find(cc => cc.tag === tagToDelete);

            if (contentControl) {
              contentControl.delete();
               await context.sync();
              console.log('Content Control deleted.');
            } else {
               console.log('No content control found with the specified tag.');
             }
        });
     } catch (error) {
       console.error('Error:', error);
   }
 }

 // Call the function to delete the Content Control with the specified tag
const tagToDelete = 'f4022b4f-f104-4e13-a86c-1f0cc359fbb7';
deleteContentControlByTag(tagToDelete);

`

0

There are 0 answers