none
C# With Word Interop - Comment Range.Text Always Returns null RRS feed

  • Question

  • Using Windows 7, Visual Studio 2015, .NET framework 4.0 (Client Profile) and 4.6, Word 2010 and Microsoft.Office.Interop.Word 14 or 15 I am unable to extract the comment text from Word document comments - the comment Range.Text property always evaluates null. My code looks like:

    using Microsoft.Office.Interop.Word;
    using System.Collections.Generic;
    
    // ...
    
    public static List<string[]> ExtractComments(Document doc)
    {
      var comments = doc.Comments;
      var commentList = new List<string[]>();
      foreach (Comment comment in comments)
      {
        var commentRow new string[4];
        string id = $"{comment.Index}";
        string author = comment.Author;
        string scope = comment.scope.Text;
        // The first three fields are present, but
        // comment.Range.Text is null unless it's viewed
        // in the debugger
        string commentText = comment.Range.Text;
        commentRow[0] = id;
        commentRow[1] = author;
        commentRow[2] = scope;
        commentRow[3] = commentText;
        commentList.Add(commentRow);
      }
      return commentList;
    }


    Strangely, if I inspect a Comment object in the debugger I can browse to its Range.Text property and see that it's present. Am I misunderstanding the Comment interface, or is there something I can do programmatically to complete instantiation of the dynamic component of a Comment object? One final point - using the OpenXML SDK I am able to retrieve all comment text from the same sample documents, however I want to use Interop to support password-protected documents. 

    Thank you.

    Friday, December 22, 2017 11:22 AM

All replies

  • Hello Flitcraft,

    I failed to reproduce your issue since I don't have Windows 7 and Visual Studio 2015. 

    I tried to test your code in Windows 10, Visual Studio 2017 and Word 2010. The code worked for me to return comment text. 

    You said that you tested the code with .NET framework 4.0 (Client Profile) and 4.6, what about other version .net Framework? Do you have any other environment? If yes, could you please try to test on these environments?

    I would try to build an environment with Windows 7, Visual Studio 2015 and Word 2010 for testing your issue. This will spend some time and your patience would be greatly appreciated.

    Besides, I would suggest you share a word document file with comments so we could use it to reproduce your issue once I build the environment.

    You could share the file via One Drive and then put link address here. Thanks for understanding.

    Best Regards,

    Terry


    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.


    Monday, December 25, 2017 7:40 AM
  • Hello Terry,

    Thanks very much for your response. When I was preparing a sample document to illustrate the condition I described, I found that setting the Visible property true when I open a document lets the extracted comments text show. I'm not positive that's the full explanation, but it has been pretty consistent for me in limited testing.

    Thanks again,

    Sean.

    Saturday, December 30, 2017 8:55 PM
  • Hello Sean,

    I'm glad to hear that you have solved the issue. I would suggest you mark your solution as answer to close this thread. If you have any other issue, please feel free to post thread to let us know.

    Best Regards,

    Terry


    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.

    Tuesday, January 2, 2018 1:13 AM
  • Same issue here, but with a field instead when calling myField.Result.Text.

    Application running on my machine is always fine. On other machines, it did not work most of the time.

    Tried to set visibility to True but did not solve my issue.

    Monday, November 5, 2018 4:31 PM