Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to automatically repair corrupt Excel workbooks?

Tags:

excel

vba

spss

I am using SPSS 15 to create several Excel reports which I am then consolidating using an Excel macro. Unfortunately that particular SPSS version produces .xls files that aren't easily readable for Excel 2007 and up. Excel 2003 gobbles those files up just fine, but newer Excel versions display two error messages. First up is "Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook?". After clicking yes this is followed by "File Error: data may have been lost". Unfortunately these error messages cause my macro to quit at the first file with error code 1004. This is the code I am using to open my Excel files:

If Len(Dir(ThisWorkbook.Path + "\filename.xls")) <> 0 Then 
    Workbooks.Open Filename:=ThisWorkbook.Path + "\filename.xls"
End If

I checked with IBM (SPSS vendors) and they told me that this particular issue is fixed in SPSS 16 but because of business reasons an upgrade is not on the cards. Now there is a manual workaround which involves opening the files and saving again but with dozens of files that's obviously no fun. Therefore I am looking for a way to automatically repair those corrupt workbooks in my macro.

Further information: we are using Excel 2010 at work, Excel 2003 is not available. A sample file is available here: https://onedrive.live.com/?cid=52106BC267261CBF&id=52106BC267261CBF!292

like image 756
Haris Avatar asked Jun 04 '14 14:06

Haris


People also ask

How to repair a corrupted Excel workbook?

Repair a corrupted workbook manually 1 On the File tab, click Open. 2 In Excel 2013 or Excel 2016, click on the location where the spreadsheet is located, and click Browse. 3 In the Open dialog box, select the corrupted workbook that you want to open. 4 Click the arrow next to the Open button, and then click Open and Repair. More items...

How to repair corrupted files on Windows?

Click on the "Open" button or just press Ctrl + O on your keyboard. 3. Locate the corrupted file on your computer's memory and select it. 4. Click on the More icon (the dropdown button) located adjacent to the Open button and choose to "Open and Repair" the file.

How to re-open a workbook in Excel?

1 Click File > Open . 2 Double-click the name of the workbook that you have open in Excel. 3 Click Yes to reopen the workbook. See More...

What is Excel file corruption?

What Is Excel File Corruption? It is when contents inside an Excel workbook go haywire. Microsoft doesn't offer a manual on Excel file corruption. A workbook can be damaged, if improperly manipulated, by any application which reads/writes OpenXML files.


1 Answers

That file seems fairly screwed up, the BIFF validation tool reports an incorrect BOF header so its impressive Excel 2003 can open it at all.

I can read your file as an ISAM database via the Jet OLEDB provider so you may choose to read the file this way (or use this approach to generate a CSV file for processing)

Dim cn As Object, rs As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=MICROSOFT.JET.OLEDB.4.0;Data Source=C:\temp\DebugFile.xls;Extended Properties=""Excel 8.0;HDR=Yes;"""

Set rs = cn.Execute("SELECT * FROM [DebugFile$]")

Do While Not rs.EOF
    Debug.Print rs.Fields(0).Value
    rs.MoveNext
Loop
like image 60
Alex K. Avatar answered Nov 15 '22 12:11

Alex K.