2016-07-07 124 views
1

我試圖從一個.docx文件複製一些評論到另一個,連同對這些評論所做的所有答覆。我刪除了非必要的部分,以免混淆你。VB將宏從一個文檔複製到另一個文檔的宏

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程序員,我似乎在這裏遇到了一堵磚牆。

+0

我不是很熟悉這個屬性,但你不應該聲明newComment作爲評論對象 –

+0

它是在我已經刪除了部分中聲明。它自己的評論是複製文件。問題似乎是調用Add newComment.Replies。 –

+0

你在什麼版本的Word? 'Comment.Replies'僅在Word 2013中可用。 –

回答

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 
相關問題