Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run-time error 4160 when trying to use Compare with VBA

569 views
Skip to first unread message

Aviator9

unread,
Oct 20, 2009, 9:20:01 AM10/20/09
to
Hi All,

I'm trying to run a word 2007 compare using VBA. I'm getting a run-time
error, bad file name. Here's the code:

Application.CompareDocuments OriginalDocument:=Documents("C:\example.docx"), _
RevisedDocument:=Documents("C:\example2.docx"), _
Destination:=wdCompareDestinationNew

Can anyone help me?

Pesach Shelnitz AT

unread,
Oct 20, 2009, 12:31:07 PM10/20/09
to
Hi,

The OriginalDocument and RevisedDocument parameters must be Document
objects. The following macro shows how to create and use these objects.

Sub CompareDocs()

Const Error_FileNotFound = 5174
Dim fileName1 As String
Dim fileName2 As String
Dim doc1 As Document
Dim doc2 As Document

fileName1 = InputBox("Type the name of the original file," _
& vbCrLf & "including its full path.")
fileName2 = InputBox("Type the name of the revised file," _
& vbCrLf & "including its full path.")
On Error Resume Next
Set doc1 = Documents.Open(fileName1)
If Err.Number = Error_FileNotFound Then
MsgBox "The original file specified was not found."
Exit Sub
End If
Err.Clear
Set doc2 = Documents.Open(fileName2)
If Err.Number = Error_FileNotFound Then
MsgBox "The revised file specified was not found."
Exit Sub
End If
On Error GoTo 0
Application.CompareDocuments OriginalDocument:=doc1, _
RevisedDocument:=doc2, _
Destination:=wdCompareDestinationNew
End Sub

--
Hope this helps,
Pesach Shelnitz

Aviator9

unread,
Oct 22, 2009, 12:48:02 PM10/22/09
to
Great thanks, it works.

Quick follow-up about error handling:

If Word deems both files to be the same, it throws up an error. How can I
find out the error number?

Many thanks.

Pesach Shelnitz AT

unread,
Oct 23, 2009, 4:28:01 AM10/23/09
to
Hi,

I ran the following macro, which compares the active document to itself and
reports any error, but no error was raised.

Sub CompareDocs2()
On Error Resume Next
Application.CompareDocuments OriginalDocument:=ActiveDocument, _
RevisedDocument:=ActiveDocument, _
Destination:=wdCompareDestinationNew
If Err.Number <> 0 Then
MsgBox Err.Number & ": " & Err.Description


Exit Sub
End If
On Error GoTo 0

End Sub

Anyway, the code demonstrates how to display the error information and exit
a macro gracefully.

--
Hope this helps,
Pesach Shelnitz

0 new messages