vba 比较多对文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19341190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare Multiple Pairs of Docs
提问by Derp Blerg
I am a professor in an English department, and my composition students often write multiple drafts of their essays. I use Word 2010 to track their changes.
我是英语系的教授,我的作文学生经常写多篇论文草稿。我使用 Word 2010 来跟踪他们的更改。
I discovered VBA code on another site (located here). I created a new macro. It prompts me correctly for the base, new, and comparison folders, but the output is null.
我在另一个站点(位于此处)上发现了 VBA 代码。我创建了一个新的宏。它正确提示我输入基本文件夹、新文件夹和比较文件夹,但输出为空。
I have the files in both the base and new folders named identically and saved in .doc format. I also set the trust center options in Word to 1) enable all macros and 2) trust access to the VBA project object model.
我在基本文件夹和新文件夹中都有相同命名的文件,并以 .doc 格式保存。我还将 Word 中的信任中心选项设置为 1) 启用所有宏和 2) 信任对 VBA 项目对象模型的访问。
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 = InputBox("Enter path to base documents:")
strFolderB = InputBox("Enter path to new documents:")
strFolderC = InputBox("Enter path for document comparisons to be saved:")
strFileSpec = "*.doc"
strFileName = Dir(strFolderA & strFileSpec)
Do While strFileName <> vbNullString
Set objDocA = Documents.Open(strFolderA & strFileName)
Set objDocB = Documents.Open(strFolderB & strFileName)
Application.CompareDocuments _
OriginalDocument:=objDocA, _
RevisedDocument:=objDocB, _
Destination:=wdCompareDestinationNew
objDocA.Close
objDocB.Close
Set objDocC = ActiveDocument
objDocC.SaveAs FileName:=strFolderC & strFileName
objDocC.Close SaveChanges:=False
strFileName = Dir
Loop
Set objDocA = Nothing
Set objDocB = Nothing
End Sub
回答by Zev Spitz
If you and your students are using Word 2010 then the extension would most likely be .docx
:
如果您和您的学生使用的是 Word 2010,那么扩展名很可能是.docx
:
strFileSpec = "*.docx"
Have you tried running the code step-by-step with the VBA debugger, to see what is the exact point of failure? See here(under the section entitled Stepping Through Code) for a simple introduction.
您是否尝试过使用 VBA 调试器逐步运行代码,以查看确切的故障点是什么?有关简单介绍,请参见此处(在标题为单步执行代码的部分下)。
回答by Derp Blerg
Some kind VBA guru figured it out for me: I need to enter a \ at the end of the folder paths. Without the final backslash, the script was not able to locate the folders, and thus it wasn't processing any files. After adding the final \ to all three folder paths, the code ran perfectly.
某种 VBA 大师为我解决了这个问题:我需要在文件夹路径的末尾输入一个 \。如果没有最后一个反斜杠,脚本将无法定位文件夹,因此它不会处理任何文件。将最后的 \ 添加到所有三个文件夹路径后,代码运行完美。
As a bonus, the code processes both .doc and .docx files. I thought I was going to have to convert all my .docx files to .doc before processing, but the code is smarter than that.
作为奖励,该代码同时处理 .doc 和 .docx 文件。我以为我必须在处理之前将所有 .docx 文件转换为 .doc,但代码比这更智能。
Eureka!
尤里卡!