Thanks for posting that, Mike.
Interesting results ...
I modified it slightly:
Private Sub Test()
Dim appExcel As Excel.Application
On Error Resume Next
Set appExcel = GetObject(, "Excel.Application")
If appExcel Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
End If
' no need for this:
'On Error GoTo 0
On Error GoTo WarnMe
appExcel.Application.DisplayAlerts = False
appExcel.Workbooks.Open ("C:\temp\bogus.xls")
appExcel.Application.Visible = True
' Added this here ... else you get the warning msgbox no matter what
Exit Sub
WarnMe:
MsgBox ("error number" & Err.Number)
'appExcel.Quit
'Set appExcel = Nothing
End Sub
I saved a file from Excel as bogus.xls then opened it in notepad and
added "CORRUPT" to the very beginning of the file.
When I ran the code, there were no error messages; when I checked on
Excel, it had happily opened the file as a text file and was displaying
all sorts of gibberish.
I edited the XLS in notepad again, removed the "CORRUPT" that I'd added
before, scrolled down a bit, selected a bunch of compugibber and
deleted it.
This time when I ran the code it behaved as you'd want it to ... error
message and all.
So the problem is that if the file itself tells Excel that it's indeed
an XLS, Excel will fail to open it and you get your trappable error
condition. If the file's corrupted in such a way that it doesn't
internally CLAIM to be an Excel file, Excel treats it as text and opens
it any which way, gives you no error msg.
I'd bet that the first few characters in the file are signature bytes
that ID the file as an XLS. You might want to read those from a
prospective file and if they aren't what they should be, refuse to open
the file in Excel. If the sig bytes ARE correct, turn it over to Excel
and let it scream about further inconsistencies, which you'll be able
to trap for.
Are we having fun yet? Please tell me we're having fun.