Macro for comparing many documents in two folders

J

jennifer72401

I have 2 folders, "folder A" and "folder B". Each folder contains files with
the same names. I want to compare, for example, "File A" from Folder A with
"File A" from Folder B, and so forth. Is there a way to automate this, so i
don't have to each one manually?

I'm not too concerned about saving each file as the compare is done, leaving
all documents open is fine. If I need to save each one, I can do that
manually.
 
D

David Horowitz

Jennifer,
Generally you can use the built-in Dir() function to loop over all files in
a folder.
To compare two documents, you can either open the source file and then use
Document.Compare to compare it against the one from Folder B, or if you are
using Word 2007, you can use the Application.CompareDocuments method
instead.
If you need further assistance with either the use of Dir() or one of the
Compare methods, let us know!
 
J

jennifer72401

I think I will need a little more guidance, I'm not too familiar with VBA. If
there's a website I can go to in order to figure it out, that would help! I
am using Office 2003 as well.

Thanks!
 
D

David Horowitz

Jennifer,
The following code works on my system. You need to fill in the correct
folder names for your situation.
If you don't know how to get started at all with VBA code, as in how to make
use of this code, write back.

'***** code begins here
Sub CompareAllFiles()
Dim strFolderA As String, strFolderB As String
Dim strFileSpec As String, strFileName As String
Dim objDocA As Word.Document
strFolderA = "C:\test folder\folderA\"
strFolderB = "C:\test folder\folderB\"
strFileSpec = "*.doc"
strFileName = Dir(strFolderA & strFileSpec)
Do While strFileName <> vbNullString
Set objDocA = Documents.Open(strFolderA & strFileName)
objDocA.Compare _
Name:=strFolderB & strFileName, _
CompareTarget:=wdCompareTargetNew
objDocA.Close
strFileName = Dir
Loop
Set objDocA = Nothing
End Sub

' ***** code ends

If you ever need to take advantage of 2007-specific compare features, you
could use this:

'*******
Option Explicit

Sub CompareAllFiles2007()
Dim strFolderA As String, strFolderB As String
Dim strFileSpec As String, strFileName As String
Dim objDocA As Word.Document, objDocB As Word.Document
strFolderA = "C:\test folder\folderA\"
strFolderB = "C:\test folder\folderB\"
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
strFileName = Dir
Loop
Set objDocA = Nothing
Set objDocB = Nothing
End Sub
' *** code ends here

Hope this gets you closer.
Let us know!
 
G

Greg Maxey

Hi David,

Perhaps just oversigth, but it seems that strFileSpec "*.doc" will might
lead to errors and exclude docx and docm files using Word2007. I normally
use something like this:

strFileSpec = "*.*"
strFileName = Dir(strFolderA & strFileSpec)
Do While strFileName <> vbNullString
Select Case UCase(Right(strFileName, 4))
Case Is = ".DOC", "DOCX", "DOCM"
'Process
Case Else
'No process
End Select
Loop
 
D

David Horowitz

Good point Greg, yep, oversight on my part. I think using strFileSpec =
"*.doc*" or "*.doc?" would work as well as using the Select Case.
Thanks for pointing this out!
 
J

jennifer72401

thank you very much!! that worked great!

David Horowitz said:
Good point Greg, yep, oversight on my part. I think using strFileSpec =
"*.doc*" or "*.doc?" would work as well as using the Select Case.
Thanks for pointing this out!
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top