none
Word fails to create a single logical undo-able operation when editing document content and custom xml content RRS feed

  • Question

  • Hi,

    We have a document generation system in place where we use Content Controls as placeholders for data. To indicate the type of data allowed within a content control we maintain a custom xml file embedded in the Word document, reachable through ThisDocument.CustomXmlParts. When a content control is added to the document using our and the underlying Word APIs we also adda a node in the custom xml part.

    What we are noticing is that Word fails to make the addition of the content control and the addition of a custom xml node a single logical operation even when we use ThisDocument.Application.UndoRecord.StartCustomRecord. Therefore, the user is able to undo the creation of the custom xml node containing our metadata, while retaining the content control in the document.

    Eg., the following code should create a single undo-able record, but running this sample gives you two undoable records.

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        // the custom xml part is added on document load as a sample
        Office.CustomXMLPart MyCustomXmlPart = Globals.ThisDocument.MyCustomXmlPart;
        // start the logical operation
        Globals.ThisDocument.Application.UndoRecord.StartCustomRecord("Do it");
        // first action is adding a content control using VSTO API's
        var control = Globals.ThisDocument.Controls.AddContentControl(
            "My Control Name", Word.WdContentControlType.wdContentControlRichText);
        // configure it a bit
        control.LockContents = true;
        control.Title = "MyNode";
        control.Tag = "Tag";
        control.SetPlaceholderText(null, null, "MyNode");
        // Create the custom metadata for the control in the embedded XML file. Use the control ID 
        // as a pointer back into the document
        Globals.ThisDocument.MyCustomXmlPart.DocumentElement.AppendChildNode("MyNode", "",
            Microsoft.Office.Core.MsoCustomXMLNodeType.msoCustomXMLNodeElement, control.ID);
        // done with the logical operation
        Globals.ThisDocument.Application.UndoRecord.EndCustomRecord();
    }

    When running this in Word, we get two undo-able operations not one.

    Any help on what's wrong or on whether this is a bug in Word?

    Thanks!

    Wouter

    Wednesday, October 8, 2014 11:25 AM

All replies

  • And interestingly enough, if you swap creating the custom xml node and creating the document content around (first do the custom XML node, then do the content control) you do get only 1 logical operation.

    Until you also start updating attributes and such.

    Also, when you use the startup event to create a custom xml part, add a node, and then call UndoClear(), the undo list is not cleared!

    Is there any working functionality surrounding custom undo records in Word?


    Wednesday, October 8, 2014 11:38 AM
  • Hi,

    Thank you for posting in the MSDN Forum.

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Sorry for any inconvenience and have a nice day!

    Best regards.

    Luna


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 9, 2014 8:15 AM
    Moderator
  • Hi Luna,

    thank you. I appreciate the time you are spending on this issue!

    Wouter

    Thursday, October 9, 2014 8:46 AM
  • Hi Wouter,

    I tried to reproduce the issue reported at my end and I could see the same behavior at our end.

     It seems the issue is for the reason Word is having a watch for CustomXMLPart content updates and creating an Undo record and it appears to be "By Design"

    Thanks

    Sangeeth


    Sangeeth,MSFT

    Monday, October 13, 2014 9:18 PM
  • I understand that you can look into the code and see that Word tracks changes to the custom xml content using undo records. All well and good. However, it also does this for normal content changes right? The question is why Word fails to see my own UndoRecord as an encapsulating record so that both changes to XML and to content can be tracked as a single change, which is the intended purpose of the UndoRecord functionality right?

    Also, what is uber strange is that the order of editing changes the way the undo button works:

    - create undo record, edit custom xml, edit content --> Single logical operation, one undo undoes all

    - create undo record, edit content, edit custom xml --> two logical operations. First undo only for the custom xml

    It seems that there is a bug here that in some cases Word loses the custom undo record scope. Why is that? And should it? My guess would be no and this is a bonafide bug in Word.

    @Luna. Please allow me to mark my own question as answered.

    Tuesday, October 14, 2014 7:56 AM
  • I have seen another manifestation of this issue, also with very strange quirks that are more akin to "by accident" than to "by design."  See https://social.msdn.microsoft.com/Forums/office/en-US/21fa34e3-bdb9-4b81-91a9-157042969ecb/unexpected-property-update-entry-in-the-undo-stack?forum=worddev.

    Tuesday, November 11, 2014 1:01 PM
  • Is anyone still looking at this issue?
    Wednesday, December 17, 2014 8:30 AM