PDA

View Full Version : Script for comparing 2 versions of Word document from a folder



coeurdange57
12-13-2017, 09:02 AM
Hi,

I create a script allowing to compare 2 versions of a same document in order to create third document showing the results:



Private Sub SummaryReportButton_Click()
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 old version of documents:")
strFolderB = InputBox("Enter path to new version of documents:")
strFolderC = InputBox("Enter path for document comparisons to be saved:")
strFileSpec = "*.docx"
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


When I execute the script, it's working only for the first report of the folder. For instance: report1.docx in the folder OLD is compared with report1.docx in the folder NEW and the file report1.docx is generated in the folder RESULTS with the comparisons - There isequally another report in the folders OLD and NEW: report2.docx. But the second report report2.docx is not created in the folder RESULTS)

Could you please help me. There is certainly an error in the script or something is missing.

Thank you in advance for your help.

Regards

Dave
12-13-2017, 09:39 PM
Trial changing...

objDocA.Close
objDocB.Close
to...

objDocB.Close SaveChanges:=False
objDocA.Close SaveChanges:=False
HTH. Dave