Word07 VBA读取修订信息Revisions出错,运行时错误'5852',要求的对象不可用
代码如下:
Public Sub ExecuteCompare(KeyDocPath As String, CompareDocDirPath As String)
Dim keyDoc As Document, comDoc As Document, newDoc As Document
'打开原始文档
Set keyDoc = Documents.Open(FileName:=KeyDocPath, Visible:=False)
Set comDoc = Documents.Open(FileName:=CompareDocDirPath, Visible:=False)
Set newDoc = Application.CompareDocuments(keyDoc, comDoc)
keyDoc.Close
comDoc.Close
WriteResultToTextFile newDoc.Revisions, Replace(CompareDocDirPath, ".docx", ".txt")
End Sub
'将不同之处记录到一个与doc文档同名的txt文件中
Private Sub WriteResultToTextFile(ResultRevisions As Revisions, FileName As String)
Dim iCount As Integer, rev As Revision
Dim FSO As New FileSystemObject, ts As TextStream
Set ts = FSO.CreateTextFile(FileName)
ts.WriteLine ("共" & CStr(ResultRevisions.Count) & "处修订:")
ts.WriteBlankLines 1
iCount = 1
For Each rev In ResultRevisions
ts.WriteLine iCount & TypeToDescription(rev.Type)
ts.Write rev.Range
ts.WriteBlankLines 2
iCount = iCount + 1
Next
ts.Close
Set ts = Nothing
End Sub
'返回用户更改的类型对应说明
Private Function TypeToDescription(revType As WdRevisionType) As String
Dim retStr As String
Select Case revType
Case WdRevisionType.wdNoRevision
retStr = "无修订"
Case WdRevisionType.wdRevisionConflict
retStr = "将修订标记为冲突"
Case WdRevisionType.wdRevisionDelete
retStr = "删除"
Case WdRevisionType.wdRevisionDisplayField
retStr = "域显示方式已更改"
Case WdRevisionType.wdRevisionInsert
retStr = "插入"
Case WdRevisionType.wdRevisionParagraphNumber
retStr = "段落编号已更改"
Case WdRevisionType.wdRevisionParagraphProperty
retStr = "段落属性已更改"
Case WdRevisionType.wdRevisionProperty
retStr = "属性已更改"
Case WdRevisionType.wdRevisionReconcile
retStr = "将修订标记为已解决的冲突"
Case WdRevisionType.wdRevisionReplace
retStr = "已替换"