none
Maintaining Comment ID's across document versions RRS feed

  • Question

  • We have been designing a tool to track the Comments made in a Word document in an external SharePoint list so that we can add business metadata around them.  Everything seemed to be going alright until we noticed that comment ID's were changing for each version of the document.

    Is there a way to maintain (or track) Comment ID's between versions of a Word Document?

    For example:

    Create a document and put in the words "Test Text"
    Create a comment on the word "Text"
    Save it as docx and close it
    Open the docx file with 7zip or your favorite zip program locate and open the document part to see that the ID for the comment on Text is 0
    Now open the document again and put a comment on the word "Test"
    Save it and close it
    Open the docx file with 7zip or your favorite zip program again, locate and open the document part to see that the ID for the comment on Text is now 1, 0 has been reassigned to the comment over "Test"

     

    • Edited by petejo Friday, February 11, 2011 10:37 PM missed a step in my example
    Friday, February 11, 2011 10:16 PM

All replies

  • Hi Petejo,

    Thank you for posting and we are glad to help with you.

    >>Is there a way to maintain (or track) Comment ID's between versions of a Word Document?

    I think you can list the comments' IDs of one version and store them in a file or database, such as you can create a table which contains fields: document version, comment run text, comment text.  I have some code snippet for you to reference which can get all the comments' IDs :

    static void ProcessComments(WordprocessingDocument doc)
      {
        StringBuilder runText = null;
        string commentId;
        StringBuilder commentText = null;
        Document mainDocument = doc.MainDocumentPart.Document;
    
        // If there are comments then display a heading for the
        // comments output.
        if (mainDocument.Descendants<CommentRangeStart>().Count() > 0)
        {
          Console.WriteLine("{0, -12} {1, -30} {2, -30}",
            "Comment Id", "Comment Run Text", "Comment Text");
          Console.WriteLine("{0, 12} {1, 30} {2, 30}",
            "------------",
            "------------------------------",
            "------------------------------");
        }
        else
        {
          Console.WriteLine(
            "The document does not contain any comments.");
          Console.WriteLine();
          return;
        }
    
        // Iterate through the commentRangeStart elements in the
        // main document part.
        foreach (CommentRangeStart commentRangeStart in
          mainDocument.Descendants<CommentRangeStart>())
        {
          runText = new StringBuilder();
          commentText = new StringBuilder();
    
          // Get the text in the document that is associated
          // with the comment. The text could be spread across
          // multiple text elements so process all the text
          // elements that are descendants of the commentRangeStart
          // element's parent paragraph.
          foreach (Text text in
            commentRangeStart.Parent.Descendants<Text>())
            runText.Append(text.InnerText);
    
          // The commentRangeStart element has an implicit
          // relationship with the Comments part.
          // Get the id of the associated comment via the
          // commentRangeStart's Id attribute.
          commentId = commentRangeStart.Id.Value;
    
          // Get the comment element from the comments part
          // via it's id.
          Comment comment = doc
            .MainDocumentPart
            .WordprocessingCommentsPart
            .Comments
            .Elements<Comment>().Single(c => c.Id == commentId);
    
          // Get the comment text. The text could be spread across
          // multiple text runs so process all the text elements
          // that are descendants of the comment element.
          foreach (Text text in comment.Descendants<Text>())
            commentText.Append(text.InnerText);
    
          // Shorten the run and comment text if needed.
          if (runText.Length > 30)
          {
            runText.Length = 27;
            runText.Append("...");
          }
    
          if (commentText.Length > 30)
          {
            commentText.Length = 27;
            commentText.Append("...");
          }
    
          // Display the comment id, document text, and comment text.
          Console.WriteLine("{0, 12} {1, -30} {2, -30}",
            commentId, runText, commentText);
        }
    
        Console.WriteLine();
      }
    

    The function can get all the comments in the word document, it is used in Console Application, you can change it to adapt to your program.

    In addition, please take a look at the article about retrieving content from document: http://msdn.microsoft.com/en-us/library/ee413542(v=office.12).aspx , it has the complete code which can get all the comments in the word document.

    >>Open the docx file with 7zip or your favorite zip program again, locate and open the document part to see that the ID for >>the comment on Text is now 1, 0 has been reassigned to the comment over "Test"

    As far as I know, the reason is that the Comment ID is reassigned based on the order of the comments in the document. Could you tell us the more detail information of your requirement? Since the Comment ID will keep changing, you may not be correctly filled if you use Comment ID as the key for each comment.

    I hope this can help you and feel free to follow up after you have tried.

    Best Regards,

     


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 14, 2011 5:04 AM
  • Hi Petejo,

    Have you resolved your issue yet? Does the suggestion work for you or not? If you still show concern on the issue, just feel free to follow up and we are glad to help with you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 16, 2011 2:38 AM
  • We are looking for a solution how to maintain unique comment Ids across the document revisions. When we are inserting a comment to document instead of assigning new Id, its recalculating Ids and changing all existing Ids. It seems Microsoft Word macro changing the comment Ids when adding new comment to the document.

    Here is the scenario why I need unique commentId to comments in Doc.

    When user uploads document to sharepoint document library (Versioning turned on)  I need to extract the comments from document.  When user sends document for review, Reviewer makes some comments and sends back to author. Now I want to compare old comments (version 1) with new comments based on comment id. For each new comment if comment id would be unique from earlier comments life would be easier to compare the comments. We are wondering why word needs to adjust the commentId instead of generating new Id? I hope you understand the problem.

    Thank you.

     

     

    Thursday, February 17, 2011 11:12 PM
  • Hi Bruce,

    First thanks for the great reply.  I also want to clarify so you don't put yourself through extra work.  We have a document library with versioning turned on.  We have created a working event receiver for uploading documents that takes any comments in the document and copies to a "current comment list".  If there were already comments for that document in the list (the document already existed and this is a new version) then the old comments get moved to a "history list" before the new comments get copied into the curent comment list.

    We have also added a few menu items to the document library so that users can go directly to the "current comment list" or "history list" from the documents themselves.

    All this is working fine. 

    So here is the scenario.  An author at a company creates a document.  The document gets emailed (this has to happen at this time) out to a reviewer elsewhere and the document comes back with editorial comments in it.  The author uploads the file to a SharePoint document library and the above mentioned process takes place.

    Next the author needs to Reply to the editorial comments.  The Reply being "what the user did about the editorial comment".  So if there was an editorial comment in a document that said "Change this text to green and underline it".  The reply might be "Green is not an approved color so we did not change it to green but we did underline it". 

    The author wants to be able to reply to the comment IN the SharePoint list next to the original comment.  Another event receiver will take the reply comment and insert it into the document next to the editorial comment.  Both these are easy enough to accomplish but this is where we start to have an issue.  Without a static ID (I would like clarity if possible why the ID's must be sorted for comments or how ID can really be applied in this case as it is more like a tag or place-marker than an actual IDentifier) how can I continue the correlation between the editorial comment and the reply comment across further versions of the document as it gets sent out to other reviewers and returned with new comments when the ID's may change at any time?

    Thanks again for the time and the great response!


    Pete Jongema
    Friday, February 18, 2011 7:49 PM
  • Hi Petejo,

    I am afraid it can't be achieved via the comment ID since it will change if the new comments are added to the document and I haven't figured out a similar method. Then as your requirement, could you use an alternative way by adding some text before the text? For instance,

    A to B's comment: A's reply comment content;

    C to A's comment: C's reply comment content;

    D to C's comment: D's reply comment content;

    ...

    The approach need to add the names of the reviewer and whose comment is going to be reviewed.

    I hope it can help you.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Bruce Song Wednesday, February 23, 2011 8:22 AM typo
    Wednesday, February 23, 2011 8:22 AM
  • Hi Petejo,

    Have you got an idea to resolve your problem yet?Does the suggestion work for you or not? If you still show any concern on this post, just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 4, 2011 3:06 AM