VBA-suppress error message for corrupt file

Laavista

Board Regular
Joined
Aug 27, 2009
Messages
79
I'm using Excel 2003.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I'm looping through files, opening them, and writing info into another Excel spreadsheet. I need to check for corrupt excel files. I'm getting close, but I'm getting the error message "this file is not in a recognizable format" and the user would have to remember to select "cancel" (and not "OK"). I'd like to suppress this error message.
<o:p> </o:p>
My code:
<o:p> </o:p>
In my sub:
<o:p> </o:p>
Global FileIsCorrupt as boolean
Global NewFileToCheck as string
Global FileName as string
Global Path as string
<o:p> </o:p>
(Filename and path are set at this point...)
<o:p> </o:p>
OpenFileAndCheck 'call function
<o:p> </o:p>
If FileIsCorrupt = True then 'if true, file is corrupt or unrecognizable
msgbox ("msg to user that it is corrupt and file is being skipped')
GoTo FoundCorruptFile ' skips over writing info from file
End if
<o:p> </o:p>
=====
Function OpenFileAndCheck() as Boolean

On Error GoTo ErrHandler
FileIsCorrupt = False 'set to false--it will be reset to true if file is corrupt

Set NewFileToCheck = Workbooks.Open(Filename:=Path & FileName)
<o:p> </o:p>
Exit Function
<o:p> </o:p>
ErrHandler:
If Err <> 0 then
FileIsCorrupt = True
End if
<o:p> </o:p>
End Function
=====
<o:p> </o:p>
Your help would be so appreciated!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I tried to leave your code intact. Here is an approach that may do.

Code:
Function OpenFileAndCheck() As Boolean
On Error GoTo ErrHandler
Set NewFileToCheck = GetWorkbook(Path & Filename)
OpenFileAndCheck = Not (NewFileToCheck = Nothing)
Exit Function
ErrHandler:
If Err <> 0 Then
FileIsCorrupt = True
End If
End Function
 
Function GetWorkbook(Fullname As String) As Workbook
    Dim wb As Workbook
    
    Application.DisplayAlerts = False
    Set wb = Workbooks.Open(Fullname)
    Application.DisplayAlerts = True
    'perform some validation here on data that should be there or even structure
    'that is consistent for all like workbooks
    
    'validation code
    
    If All Is well Then
        Set GetWorkbook = wb
    Else
        wb.Close
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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