R
Rich
I have to read data from an Excel Sheet. Using Microsoft Office Interop and
Automation I create an Excel object
Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range
I open a workbook and then read the data from a worksheet using the Sheet's
UsedRange property into an Excel Range object I call rng. Then I loop
through all the cells in this rng object. This works fine except if there is
no data in a particular cell.
Try
....
For i As Integer = 1 to rng.Rows.Count
For j As Integer= 1 to rng.Columns.Count
str1 = Ctype(rng(i, j), Excel.Range).Value.ToString
Next
Next
....
Catch ex1 As Exception
....
End Try
The loop aboves works fine if there is data in a given cell/range. But
bombs if there is no data (crashes the app) even with the Try/Catch. So I
added another Try/Catch inside the loop
For i As Integer = 1 to rng.Rows.Count
For j As Integer= 1 to rng.Columns.Count
Try
str1 = Ctype(rng(i, j), Excel.Range).Value.ToString
...
Catch ex As Except
str1 = "**"
...
End Try
Next
Next
In the system error trap (after the app crashed without the inner
try/catch) the message said the value which crashed the app was 'undefined'.
I tried
If ctype(rng(i, j), Excel.Range).value Is Nothing Then ...
but this did not keep the app from crashing. Is there a way to trap for
'Undefined' besides Try/Catch or is Try/Catch the desired method?
Thanks,
Rich
Automation I create an Excel object
Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range
I open a workbook and then read the data from a worksheet using the Sheet's
UsedRange property into an Excel Range object I call rng. Then I loop
through all the cells in this rng object. This works fine except if there is
no data in a particular cell.
Try
....
For i As Integer = 1 to rng.Rows.Count
For j As Integer= 1 to rng.Columns.Count
str1 = Ctype(rng(i, j), Excel.Range).Value.ToString
Next
Next
....
Catch ex1 As Exception
....
End Try
The loop aboves works fine if there is data in a given cell/range. But
bombs if there is no data (crashes the app) even with the Try/Catch. So I
added another Try/Catch inside the loop
For i As Integer = 1 to rng.Rows.Count
For j As Integer= 1 to rng.Columns.Count
Try
str1 = Ctype(rng(i, j), Excel.Range).Value.ToString
...
Catch ex As Except
str1 = "**"
...
End Try
Next
Next
In the system error trap (after the app crashed without the inner
try/catch) the message said the value which crashed the app was 'undefined'.
I tried
If ctype(rng(i, j), Excel.Range).value Is Nothing Then ...
but this did not keep the app from crashing. Is there a way to trap for
'Undefined' besides Try/Catch or is Try/Catch the desired method?
Thanks,
Rich