比较word文件时出错 [英] Error while comparing word files
问题描述
我是这个VBA的新手,并且遇到了一些有问题的代码,希望你能提供帮助。
Hi, I am a newbie to this VBA and have been struck with a code with some issue and hope you can help.
在下面的代码中,我试图将第一个文件夹中的word文档与第二个文件夹中的修订版本进行比较,并将比较文档保存在第三个文件夹中。代码运行得非常好,现在编译或运行时错误。
In the code below I am trying to compare a word document in 1st folder with its revised version in the 2nd folder and saving the compared document in 3rd folder. The code is running perfectly fine and now compile or run time errors.
但运行后只有一个比较文件被保存到第三个文件夹,即使我每个文件中有3个文件 文件夹仅比较每个文件夹中的第一个并保存在第3个文件夹中。此外,结果文件夹
中保存的文件未被打开,说明该文件存在问题。
But after running it only one compared file is being save to the 3rd folder i.e., even though I have 3 files in each folder only the first one from each folder is being compared and being saved in the 3rd folder. Also the saved file in the result folder is not being opened stating that there is some problem with that file.
任何建议或想法如何解决此问题?
Any suggestions or ideas how can I resolve this?
推荐答案
嗨tony9854,
我可以重现您的问题,我测试了测试文档中的代码。运行此代码后,结果文件夹中只有一个文件,测试文档将关闭,
在我的测试,在Set objDocC = ActiveDocument行,activedocument将引用测试文档,因此它将被关闭并且代码停止运行,是否和你一样?
Application.CompareDocuments 返回一份文件,我认为应该是objDocC。所以我更新了这样的代码
Application.CompareDocuments return a document and I think it should be objDocC. So I updated the code like this
Sub CompareAllFiles()
Dim strFolderA As String
Dim strFolderB As String
Dim strFolderC As String
Dim strFileSpec As String
Dim strFileName As String
Dim objDocA As Word.Document
Dim objDocB As Word.Document
Dim objDocC As Word.Document
strFolderA = "C:\Users\Desktop\T\old\"
strFolderB = "C:\Users\Desktop\T\new\"
strFolderC = "C:\Users\Desktop\T\result\"
strFileSpec = "*.docx"
strFileName = Dir(strFolderA & strFileSpec)
Do While strFileName <> vbNullString
Set objDocA = Documents.Open(strFolderA & strFileName)
Set objDocB = Documents.Open(strFolderB & strFileName)
Set objDocC = Application.CompareDocuments( _
OriginalDocument:=objDocA, _
RevisedDocument:=objDocB, _
Destination:=wdCompareDestinationNew)
objDocA.Close
objDocB.Close
objDocC.SaveAs FileName:=strFolderC & strFileName
objDocC.Close SaveChanges:=False
strFileName = Dir
Loop
Set objDocA = Nothing
Set objDocB = Nothing
End Sub
最好的问候,
特里
这篇关于比较word文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!