VB Script to compare word documents

anbillava

New Member
Joined
Nov 16, 2014
Messages
4
I have two locations, one has the old files and the other one has the revised files. I am trying to create a vb macro to compare each of these word document and save them in a new location.


I am not able to succeed can somebody help me.
Sub Macro1()
'
' Macro1 Macro
'


Set objWord = CreateObject("Word.Application")
objWord.DisplayAlerts = wdAlertsNone
objWord.Visible = True

Set ORIGINAL = objWord.Documents.Open("S:\Data Management\Oncology Data Management\DataOps\Global Projects\RTF Comparison\Input\old\Profile_10011002.rtf", , False)
Set REVISED = objWord.Documents.Open("S:\Data Management\Oncology Data Management\DataOps\Global Projects\RTF Comparison\Input\new\Profile_10011002.rtf", , False)

Application.CompareDocuments OriginalDocument:=ORIGINAL, _
RevisedDocument:=REVISED, _
Destination:=wdCompareDestinationNew

'ORIGINAL.Close False
'REVISED.Close False
'objWord.Quit


End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Here you should be using objWord instead of Application.
Code:
Application.CompareDocuments OriginalDocument:=ORIGINAL, _
RevisedDocument:=REVISED, _
Destination:=wdCompareDestinationNew
Click to see how the code should look.

Code:
objWord.CompareDocuments OriginalDocument:=ORIGINAL, _
RevisedDocument:=REVISED, _
Destination:=wdCompareDestinationNew
 
Upvote 0
I am able to improvise my code as below. However your response to change to Objword does not really make any difference. Thank you very much for your response.

I am getting an prompt window after the comparison saying the "table in the document has become corrupted". This is annoying. Is there a way I can bypass those prompt. I appreciate your help.

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:")
strFolderA = strFolderA & "\"
strFolderB = InputBox("Enter path to new documents:")
strFolderB = strFolderB & "\"
strFolderC = InputBox("Enter path for document comparisons to be saved:")
strFolderC = strFolderC & "\"
strFileSpec = "*.rtf"
strFileName = Dir(strFolderA & strFileSpec)
'MsgBox strFolderA
Do While strFileName <> vbNullString


Set objDocA = Documents.Open(strFolderA & strFileName)
Set objDocB = Documents.Open(strFolderB & strFileName)
Application.CompareDocuments _
OriginalDocument:=objDocA, _
RevisedDocument:=objDocB, _
Destination:=wdCompareDestinationNew, _
IgnoreAllComparisonWarnings:=False


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
 
Upvote 0
You still aren't using objWord, and it also needs to be added here.
VBA Code:
Set objDocA = Documents.Open(strFolderA & strFileName)
Set objDocB = Documents.Open(strFolderB & strFileName)
Documents.Open should be objWord.Docuements.Open.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top