Search code examples
macrosvbscriptms-word

When exporting Word review comments, how do you reference the sentence related to a comment?


I am trying to export a Word document's review comments. I want to export the sentence selection that was commented on followed by the comment.

Screen shot of the image: http://jspeaks.com/mswordcomment.png

I have found code to loop through the document comments, but I cannot figure out how to reference the sentence selection that the comment was related to.

The current logic is:

Sub ExportComments()
    Dim s As String
    Dim cmt As Word.Comment
    Dim doc As Word.Document

    For Each cmt In ActiveDocument.Comments
        s = s & cmt.Initial & cmt.Index & "," & cmt.Range.Text & vbCr
    Next

    Set doc = Documents.Add
    doc.Range.Text = s
End Sub

I tinkered with Selection.Range, however I cannot determine the proper object or property that contains the referenced sentence.

I would like to produce output like the following (if we use the example in picture above):

Sentence: Here are more sentences that contain interesting facts - Comment: This is an interesting fact. Sentence: Here are more sentences that contain interesting facts. Here are more sentences that contain interesting facts. - Comment: This is a very interesting fact


Solution

  • I found someone on another site to solve this question.

    The key to the solution is: cmt.Scope.FormattedText

    Here is the function revised:

    Sub ExportComments()
        Dim s As String
        Dim cmt As Word.Comment
        Dim doc As Word.Document
    
        For Each cmt In ActiveDocument.Comments
            s = s & "Text: " & cmt.Scope.FormattedText & " -> "
            s = s & "Comments: " & cmt.Initial & cmt.Index & ":" & cmt.Range.Text & vbCr
        Next
    
        Set doc = Documents.Add
        doc.Range.Text = s
    End Sub