I have a program that looks at a list of .dwg files in an excel file, opens the drawings one at a time, pulls the properties for given block references, then dumps those properties into a new sheet in the original excel file.
This code runs perfectly fine roughly half the time, with all the data accounted for and formatted properly, but the other half of the time I get Run-time Errors 9, 91 or 462.
I put the errors in parentheses in the line they're thrown.
What in my code is causing these errors, and if it's not a programming issue then what's the best way to write an ErrorHandler for these errors?
Set ExcelApp = GetObject(, "Excel.Application")
If ExcelApp Is Nothing Then
Set ExcelApp = CreateObject("Excel.Application")
If Not ExcelApp Then
ExcelApp.Workbooks.Open (tempName)
End If
End If
ExcelApp.DisplayAlerts = False
ExcelApp.Visible = False
Set wBook = ExcelApp.ActiveWorkbook
DoEvents
shtCount = wBook.Worksheets.Count ' ' (Error 91)
If shtCount <> 1 Then
DoEvents
wBook.Worksheets("Dump").Delete ' ' (Error 9, Despite that sheet existing)
DoEvents
End If
Set wSheet = wBook.Worksheets.Add(After:=Worksheets("DrawSheets")) ' ' (Error 462)
DoEvents
wSheet.Name = "Dump"
DoEvents