导出Word评论评论时,如何引用与评论相关的句子?

时间:2010-04-29 22:42:24

标签: macros vbscript ms-word

我正在尝试导出Word文档的评论评论。我想导出评论的句子选择,然后是注释。

图片的屏幕截图:http://jspeaks.com/mswordcomment.png

我找到了循环文档注释的代码,但我无法弄清楚如何引用注释与之相关的句子选择。

目前的逻辑是:

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

我使用Selection.Range修改,但是我无法确定包含引用句子的正确对象或属性。

我想生成如下输出(如果我们使用上图中的示例):

句子:这里有更多包含有趣事实的句子 - 评论:这是一个有趣的事实。 句子:这里有更多包含有趣事实的句子。这里有更多包含有趣事实的句子。 - 评论:这是一个非常有趣的事实

2 个答案:

答案 0 :(得分:8)

我在另一个网站上找到了解决这个问题的人。

解决方案的关键是:cmt.Scope.FormattedText

以下是修订的功能:

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

答案 1 :(得分:3)

我收集了几段代码并找到了解决方案:

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

来自Microsoft Answers

的最有价值的帮助