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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run-time error 4160 when running Word compare in VBA 2

Status
Not open for further replies.

Aviator9

Programmer
Oct 20, 2009
8
US
Hi All,

I'm trying to use Word VBA to run a compare using the script below:

---
Code:
Sub Macro1()

    ChangeFileOpenDirectory "C:\"
    Application.CompareDocuments OriginalDocument:=Documents("example.doc"), _
        RevisedDocument:=Documents("example2.doc"), Destination:= _
        wdCompareDestinationNew, Granularity:=wdGranularityWordLevel, _
        CompareFormatting:=True, CompareCaseChanges:=True, CompareWhitespace:= _
        True, CompareTables:=True, CompareHeaders:=True, CompareFootnotes:=True, _
        CompareTextboxes:=True, CompareFields:=True, CompareComments:=True, _
        CompareMoves:=True, RevisedAuthor:="cohen_a", IgnoreAllComparisonWarnings _
        :=False
End Sub
---

I'm receiving Run-time error 4160 / Bad file name. Can anyone help?

Thanks,
Aviator9
 
I believe the highlighted portion is your problem:
Sub Macro1()

ChangeFileOpenDirectory "C:\"
Application.CompareDocuments OriginalDocument:=Documents("[/HIGHLIGHT]example.doc[HIGHLIGHT]"), _
RevisedDocument:=Documents("[HIGHLIGHT]example2.doc[/HIGHLIGHT]"), Destination:= _
wdCompareDestinationNew, Granularity:=wdGranularityWordLevel, _
CompareFormatting:=True, CompareCaseChanges:=True, CompareWhitespace:= _
True, CompareTables:=True, CompareHeaders:=True, CompareFootnotes:=True, _
CompareTextboxes:=True, CompareFields:=True, CompareComments:=True, _
CompareMoves:=True, RevisedAuthor:="cohen_a", IgnoreAllComparisonWarnings _
:=False
End Sub

That's what the error message is trying to tell you, but it's just not showing you where it is.

--

"If to err is human, then I must be some kind of human!" -Me
 
And for future VBA questions, you need to post them over in forum707.

--

"If to err is human, then I must be some kind of human!" -Me
 
Thank you for the reply.

Those files exist in the specified folder. The macro was recorded and the result was fine. Also, I ran the same in word 2003 and it worked too.

I'm still under the assumption that whatever is recorded should run! :)
 
So, what version of Word are you running to get the error, 2007? Did you record the macro in 2003 or 2007 or what version?

--

"If to err is human, then I must be some kind of human!" -Me
 
oops, yes the error appears in 2007. I started out in 2003 where it worked. Now in 2007 for some reason it's throwing up that error.

I recorded the macros in both version separately. I also removed the normal.dot template file so 2007 is not running the old code. It's running normal.dotm where the 2007 is recorded.
 
Here's an old thread that might help:

It's an old thread, and so it's not really for Word 2007, but I figured it's worth a look.

However, when I glanced at their code, I thought of a possible problem. Are these files saved in Word 2007 format, or Word 2003 format? If Word 2007 format, the file name is different. It'll be DocumentTitle.doc[highlight]x[/highlight].

--

"If to err is human, then I must be some kind of human!" -Me
 
I checked that thread which is what got me uninstalling 2003 and the normal.dot file. The files are Word 2003 and the macro recording picked them up alright. I also just tried saving them as docx and still same error. Btw, thanks for all your help!
 
What I meant about the file extension was that you might be saving them in one file format, and then trying to reference them in your VBA code with another extension. I'd imagine either would work, but you have to make sure you reference the same file type in your VBA as you actually have saved.

Also, one other piece to look into. Some VBA items from 2003 don't work at all in 2007. At least, that's the way it is in Access. It's worth looking into.

--

"If to err is human, then I must be some kind of human!" -Me
 
The problem got fixed thanks to Pesach Shelnitz:

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

Code:
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
 
Glad you got it straitened out and thanks for the follow-up.

--

"If to err is human, then I must be some kind of human!" -Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top