Richard U
Active Member
- Joined
- Feb 14, 2006
- Messages
- 406
- Office Version
- 365
- 2016
- 2010
- 2007
- Platform
- Windows
We are upgrading to office 365 and excel 2016, and changing our directory structure.
As part of this, we need to document which spreadsheets have links. I am literally checking tens of thousands of files.
I created a crawler to scour the directories, open the files and documenting the links
the problem comes when my crawler encounters a corrupt file. Excel crashes HARD and I lose everything.
I have tried error handling like this
but it doesn't seem to be working.
It crashes right at this line:
As part of this, we need to document which spreadsheets have links. I am literally checking tens of thousands of files.
I created a crawler to scour the directories, open the files and documenting the links
Code:
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim currentPath As String, directory As Variant
Dim dirCollection As Collection
Set dirCollection = New Collection
Dim wb As New Workbook
Dim aLinks As Variant
Dim lng As Long
'Dim lng_ctr As Long
currentPath = Dir(path, vbDirectory)
Do Until currentPath = vbNullString
Debug.Print currentPath
If InStr(Right(currentPath, Len(currentPath) - InStrRev(currentPath, ".") + 1), ".xls") <> 0 Then
'If Right(currentPath, 4) = ".xls" Then
With ThisWorkbook.Sheets(1)
On Error Resume Next
Application.EnableEvents = False
Set wb = Workbooks.Open(path & currentPath)
If Err.Number <> 0 Then Set wb = Nothing
Application.EnableEvents = True
If Err.Number <> 0 Then
.Cells(lng_ctr, 1) = path
.Cells(lng_ctr, 2) = currentPath
.Cells(lng_ctr, 3) = "Could not open file"
.Cells(lng_ctr, 4) = "error number " & Err.Number
lng_ctr = lng_ctr + 1
On Error GoTo 0
Else
aLinks = wb.LinkSources(xlExcelLinks)
If Not IsEmpty(aLinks) Then
.Cells(lng_ctr, 1).Value = path
For lng = 1 To UBound(aLinks)
lng_ctr = lng_ctr + 1
.Cells(lng_ctr, 1).Value = path
.Cells(lng_ctr, 2).Value = currentPath
.Cells(lng_ctr, 3).Value = aLinks(lng)
Next lng
Else
.Cells(lng_ctr, 3).Value = "NO LINK FOUND"
.Cells(lng_ctr, 1).Value = path
.Cells(lng_ctr, 2).Value = currentPath
lng_ctr = lng_ctr + 1
End If
wb.Close
End If
End With
End If
Loop
the problem comes when my crawler encounters a corrupt file. Excel crashes HARD and I lose everything.
I have tried error handling like this
Code:
Application.EnableEvents = False
Set wb = Workbooks.Open(path & currentPath)
If Err.Number <> 0 Then Set wb = Nothing
Application.EnableEvents = True
but it doesn't seem to be working.
It crashes right at this line:
Code:
Set wb = Workbooks.Open(path & currentPath)