VB宏将单词注释从一个文档复制到另一个文档

时间:2016-07-07 12:58:52

标签: vba ms-word

我试图将.docx文件中的一些注释复制到另一个文件,以及对这些注释所做的所有回复。我删除了非必要的部分,以免混淆你。

Dim sourceDoc As Word.Document
Dim targetDoc As Word.Document

Set sourceDoc = GetObject("E:\tests\src.docx")
Set targetDoc = GetObject("E:\tests\dest.docx")

For Each comment In sourceDoc.Comments        
            text = comment.Scope.text
            comment.Scope.Select          
            Set range = targetDoc.range(comment.Scope.Start, comment.Scope.End)

            range.Expand (wdParagraph) ' Paragraphs(1).range
            range.Select                 

            f.Execute FindText:=text                

             Set newComment = Selection.Comments.Add(range:=Selection.range)
             newComment.range.FormattedText = comment.range.FormattedText
             newComment.Author = comment.Author
             newComment.Initial = comment.Initial

             For i = 1 To comment.Replies.Count
                newComment.Replies.Add (comment.Replies(i))
             Next i             

    Next comment

除Replies.Add()部分外,一切正常。我收到编译错误:对象不支持此属性或方法。我不是一个vba程序员,我似乎在这里打了一堵砖。

1 个答案:

答案 0 :(得分:1)

根据MSDNReplies.Add方法需要Range和可选的Text作为参数。不能使用Comment作为参数直接调用它。

示例:

Sub AddComment() 
 Selection.Collapse Direction:=wdCollapseEnd 
 ActiveDocument.Comments(1).Replies.Add _ 
 Range:=Selection.Range, Text:="review this" 
End Sub