Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.CompareDocuments

Status
Not open for further replies.

Misazap

Programmer
Jun 19, 2015
4
CZ
Hi!
I am new here. I sometimes need to use VBA in Word 2007 (i am not a great programmer) and now i need to write VBA for comparing files in two folders (both includes the same files, one new and second older). I need to automaticaly compare it, and for the best, in third folder create documents with changes (where.
But i cannot write the method Application.CompareDocuments without opening documents (it doesn't work). If i opened dokuments, then it works, but for 700 files it will be very slowly. There is one more complication, these documents are made from external program and when is the document opened in the Word, the Word ask for contacting the database, i must press Esc, then is the document opened (type is *.rtf).

This works:
*** Code Start
Sub porovnani_souboru()
'
' porovnani_souboru Makro
'
'
Dim myFile As String
Dim Original As Document
Dim Copy As Document

myFile = Dir("C:\TestA\*.rtf")

While myFile <> ""
Set Original = Documents.Open("C:\TestA\" & myFile)
Set Copy = Documents.Open("C:\TestB\" & myFile)

Application.CompareDocuments OriginalDocument:=Original, _
RevisedDocument:=Copy, _
Destination:=wdDompareDestinationNew, _
Granularity:=wdGranularityCharLevel, _
CompareFormatting:=False, _
CompareCaseChanges:=False, _
CompareWhitespace:=False, _
CompareTables:=False, _
CompareHeaders:=False, _
CompareFootnotes:=False, _
CompareTextboxes:=True, _
CompareFields:=True, _
CompareComments:=True, _
CompareMoves:=True, _
RevisedAuthor:="", _
IgnoreAllComparisonWarnings:=False

Original.Close SaveChanges:=True
Copy.Close SaveChanges:=True
myFile = Dir()
Wend
End Sub
*** Code End

But how write it without opening Documents? (Documents.open)
And how use Destination:=wdDompareDestinationNew to create a new file? this code will change the original (Test A)

Thanks!
Sorry for my english, i am not native ...
 
I don't know if this would help, but wouldn't it be more efficient to check if the files were identical (e.g., had the same date modified, size) before you ran a Compare Documents?
 
No, i can't use date of modifing, because every file was modified. I use styles for modifing files, but sometimes is original file changed (not by me) and then i must change my final file. From 150 changed files by date was only 10 changed by text. I need to find only the 10 changed files. If anybody do not have any idea how to make it better, i can use my old code, it is difficult, but it works. Better this than nothing.
 
You could possibly save some time via a VBA binary comparison of the files before opening them in Word. See, for example: If this comparison shows the files are the same, there's no need to open them in Word. Whether you actually save any time depends on what proportion of the files is likely to have changes, since any file that differs will have to be opened twice - once for the binary comparison, then in Word - to generate the comparison output you're after.

Cheers
Paul Edstein
[MS MVP - Word]
 
Ok, many thanks.
One more question, can i open the document and say to Word that it can ingnore its question about SQL database (in Makro).
When is the document opened, Word asks that needs to contact database. There are answers "Yes" or "No". I can use "No" or ESC.
I need something like Document.Open Question:=false ;-)
But some documents are opened without question.
Thank you
 
Setting Application.DisplayAlerts = False before opening the documents will suppress that message. Just don't save the affected document when closing, as that approach causes Word to convert the document from a mailmerge main document to an ordinary document (but with the mergefields in situ). Reset to True before exiting the macro, too.


Cheers
Paul Edstein
[MS MVP - Word]
 
Thank you!
I set it to false before opening dokuments and set back after opening. It works good. Nice :)
Misa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top