Hi,
Trying to figure out why OLEException happens.
In the below listboxToOut method, listbox contents are written out to a external text file and then Excel loads the file.
It works. However, sometimes after Excel file is displayed on the window screen, suddenly main application gets crashed.
Very annoying…
I want to find the root cause, and if impossible, at least I would like to avoid the Main application crash.
Put ‘Return True’ code in App.UnhandledException, I can’t avoid it.
Could you check the code below, and let me know how to avoid application crash?
Something wrong in code?
listboxToOut
Dim excel As New ExcelApplication
Dim book As ExcelWorkbook
Dim OutPutDirectory As String
OutPutDirectory = InstallDirectoryForDB + "\\Output\"
Dim Documents As FolderItem = GetFolderItem(OutPutDirectory)
If Documents <> Nil Then
Dim ExcelOutputName As String
Dim RightNow as new Date
ExcelOutputName = "ExcelOut_" + listboxname + "_" + TimeStamp(RightNow,True) + ".xls"
Dim f As FolderItem = Documents.Child(ExcelOutputName)
If f <> Nil Then
Try
Dim excelOut As TextOutputStream = TextOutputStream.Create(f)
If excelOut <> nil then
'For hide first dummy column
excelOut.Write chr(9) 'Tab key
For i As Integer = 1 To dataList.ColumnCount - 1
excelOut.Write Datalist.Heading(i) + chr(9) 'Tab key
Next
excelOut.Write EndOfLine
excelOut.Write Datalist.cell(-1,-1)
excelOut.Close
End if
excelOut = Nil
Catch err As IOException
Logging ( err.Message+". Error Code: "+Str(err.errorNumber) )
MsgBox err.Message+". Error Code: "+Str(err.errorNumber)
End Try
// Workaround to avoid Exception
excel.visible = false
excel.Workbooks.Open(f.AbsolutePath)
dim alphabet() as String = Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")
Dim MyCell As String
MyCell = alphabet(datalist.columnCount-1) + "1"
excel.Cells.EntireColumn.AutoFit
excel.range("A1",MyCell).Interior.ColorIndex = 1 // Cell Back Color Black
excel.range("A1",MyCell).Font.ColorIndex = 2 // Font Color White
excel.range("A1",MyCell).EntireColumn.AutoFit()
excel.visible = True
excel = Nil
Else
End if
End If
Exception err as OLEException
MsgBox err.message + " Please try it again "
App.UnhandledException
If error <> Nil Then
Dim type As String = Introspection.GetType(error).Name
Logging(type + EndOfLine + EndOfLine + Join(error.Stack, EndOfLine))
Return True
End If
If error isa OutOfBoundsException Then
Return true
End if
One more thing:
Why we don’t see ‘Please try it again’ message when Application crashed?
As you can see, there is a exception handling code.
Exception err as OLEException
MsgBox err.message + " Please try it again "