When exporting Word comments, how do you refer to a sentence related to a comment?

I am trying to export comments to view Word documents. I want to export the proposal selection that has been commented out, followed by a comment.

Image Screenshot: http://jspeaks.com/mswordcomment.png

I found code to loop through comments on a document, but I cannot figure out how to refer to the sentence selection associated with the comment.

Current logic:

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 was messing around with Selection.Range, however I cannot determine the appropriate object or property containing the specified sentence.

I would like to conclude as follows (if we use the example in the figure above):

Sentence: Here are more sentences containing interesting facts. Comment: This is an interesting fact. Suggestion: Here are more suggestions containing interesting facts. Here are more suggestions containing interesting facts. - Comment: This is a very interesting fact.

+4
source share
2 answers

I found someone on another site to resolve this issue.

Key to the solution: cmt.Scope.FormattedText

Here is the edited function:

 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 
+8
source

I collected some code snippets 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

+3
source

Source: https://habr.com/ru/post/1308414/


All Articles