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

17,594

Solution 1

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

Solution 2

I have gathered several pieces of code and came to this solution:

Sub CopyCommentsToExcel()
'Create in Word vba
'TODO: set a reference to the Excel object library (Tools --> Reference --> Microsoft Excel 12.0 Object library)

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim i As Integer
Dim HeadingRow As Integer
HeadingRow = 3

Dim cmtRef As Range

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Add ' create a new workbook
With xlWB.Worksheets(1)
' Create report info
    .Cells(1, 1).Formula = "Reviewed document:"

' Create Heading
    .Cells(HeadingRow, 1).Formula = "Index"
    .Cells(HeadingRow, 2).Formula = "Page"
    .Cells(HeadingRow, 3).Formula = "Line"
    .Cells(HeadingRow, 4).Formula = "Comment"
    .Cells(HeadingRow, 5).Formula = "Reviewer"
    .Cells(HeadingRow, 6).Formula = "Date"
    For i = 1 To ActiveDocument.Comments.Count
        .Cells(2, 1).Formula = ActiveDocument.Comments(i).Parent
        .Cells(i + HeadingRow, 1).Formula = ActiveDocument.Comments(i).Index
        .Cells(i + HeadingRow, 2).Formula = ActiveDocument.Comments(i).Reference.Information(wdActiveEndAdjustedPageNumber)
        .Cells(i + HeadingRow, 3).Formula = ActiveDocument.Comments(i).Reference.Information(wdFirstCharacterLineNumber)
        .Cells(i + HeadingRow, 4).Formula = ActiveDocument.Comments(i).Range
        .Cells(i + HeadingRow, 5).Formula = ActiveDocument.Comments(i).Initial
        .Cells(i + HeadingRow, 6).Formula = Format(ActiveDocument.Comments(i).Date, "dd/MM/yyyy")
        '        .Cells(i + 1, 3).Formula = ActiveDocument.Comments(i).Parent
        '        .Cells(i + 1, 3).Formula = ActiveDocument.Comments(i).Application
        '        .Cells(i + 1, 7).Formula = ActiveDocument.Comments(i).Author
    Next i
End With
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

Most valuable help from Microsoft Answers

Share:
17,594
jspeaks
Author by

jspeaks

Senior Software Developer - Object Oriented JavaScript Specialist Building innovative, JavaScript based enhancements within powerful web applications Rebuilt AutoTrader's Vehicle Description Page JavaScript Component - $1B Revenue Per Year Former Yahoo! Engineer (9yrs) Founding team member of a Benchmark funded startup sold to Yahoo! in 1998

Updated on June 15, 2022

Comments

  • jspeaks
    jspeaks almost 2 years

    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