none
"Ghost" element in document after removing content control RRS feed

  • Question

  • Hi,

    in our Word addin (using JS API for Office), we have functionality to add content controls and insert HTML into them, which works as expected. However, we also have the possibility to remove these content control from the addin, which causes some strange behaviour. The content control is actually removed from the document, but there is a ghost element still in the document. If I save the document and open it again, the element is gone. I cannot find any traces of the element in the ooxml using Open XML Productivity Tool.

    The specific scenario is that we add a new content control and then insert a HTML unordered list into the content control using insertHTML function. Below is a sample HTML.

    <body><ul style="list-style-type: disc;"><li style="font-family: Times New Roman;font-size: 16px;margin-top: 0px;margin-bottom: 0px;" value="1"><span style="font-family: Times New Roman;">One</span></li><li style="font-family: Times New Roman;font-size: 16px;margin-top: 0px;margin-bottom: 0px;" value="2"><span style="font-family: Times New Roman;">Two</span></li><li style="font-family: Times New Roman;font-size: 16px;margin-top: 0px;margin-bottom: 0px;" value="3"><span style="font-family: Times New Roman;">Three</span></li></ul><p style="font-family: Times New Roman;font-size: 16px;margin-top: 0px;margin-bottom: 0px;"><span>&nbsp;</span></p></body>

    This shows as a bullet list within the new content control as expected.

    At a later point we delete the content control using the delete function on content control. This also works as expected and the content control is gone from the document. However, an empty bullet point is still visible in the document. If I save and close the document and then open the document again, the empty bullet point is gone.

    Screenshots:

    After inserting the content control:

    After deletion of the content control:

    Does anyone have any insight on this issue?

    Best regards,

    Geir Morten Hagen

    Wednesday, December 20, 2017 10:03 AM

Answers

  • Hi Geir Morten Hagen,

    I again try to make a test on this issue.

    I find that contentControlObject.clear(); will help you to solve the issue.

    contentControlObject.clear();

    clear():-

    Clears the contents of the content control. The user can perform the undo operation on the cleared content.

    Example:

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
        
        // Create a proxy object for the content controls collection.
        var contentControls = context.document.contentControls;
        
        // Queue a command to load the content controls collection.
        contentControls.load('text');
         
        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            
            if (contentControls.items.length === 0) {
                console.log("There isn't a content control in this document.");
            } else {
                
                // Queue a command to clear the contents of the first content control.
                contentControls.items[0].clear();
                // Synchronize the document state by executing the queued commands, 
                // and return a promise to indicate task completion.
                return context.sync().then(function () {
                    console.log('Content control cleared of contents.');
                });      
            }
                
        });  
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });

    Output:

    So to solve your issue, you first need to clear the Content Control and then you can try to delete the Content Control.

    So after deleting the Content Control, there will be no any bullet will remain on screen.

    Reference:

    ContentControl Object (JavaScript API for Word)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 17, 2018 6:18 AM
    Moderator

All replies

  • Hi Geir Morten Hagen,

    you had mentioned that the ghost element is there after deleting the content control or a bullet point is there after using the delete function.

    it looks like that something is stored temporarily or not get updated completely when the document is open.

    so whenever you reopen it. the elements get removed and it looks properly.

    the work around can be to refresh the document in any way.

    I try to find the method or function to refresh the document but looks like currently no any function is available for the same.

    further, I will try to find any other work around for the issue.

    if I get anything useful then I will try to inform you about that.

    Thanks for your understanding.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 21, 2017 9:36 AM
    Moderator
  • Hi,

    do you have any updates on this issue?

    Best regards,
    Geir Morten Hagen

    Tuesday, January 16, 2018 2:09 PM
  • Hi Geir Morten Hagen,

    I again try to make a test on this issue.

    I find that contentControlObject.clear(); will help you to solve the issue.

    contentControlObject.clear();

    clear():-

    Clears the contents of the content control. The user can perform the undo operation on the cleared content.

    Example:

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
        
        // Create a proxy object for the content controls collection.
        var contentControls = context.document.contentControls;
        
        // Queue a command to load the content controls collection.
        contentControls.load('text');
         
        // Synchronize the document state by executing the queued commands, 
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            
            if (contentControls.items.length === 0) {
                console.log("There isn't a content control in this document.");
            } else {
                
                // Queue a command to clear the contents of the first content control.
                contentControls.items[0].clear();
                // Synchronize the document state by executing the queued commands, 
                // and return a promise to indicate task completion.
                return context.sync().then(function () {
                    console.log('Content control cleared of contents.');
                });      
            }
                
        });  
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });

    Output:

    So to solve your issue, you first need to clear the Content Control and then you can try to delete the Content Control.

    So after deleting the Content Control, there will be no any bullet will remain on screen.

    Reference:

    ContentControl Object (JavaScript API for Word)

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 17, 2018 6:18 AM
    Moderator
  • Thank you very much!
    Wednesday, January 17, 2018 8:40 AM