473,480 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

If 'undefined' then ... VS Try / Catch - how to trap 'undefined

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
Sep 29 '08 #1
4 4324
I forgot to mention that the Inner Try/Catch did keep the app from crashing.
So the inner Try/Catch did work! My question is if there is a way to trap
for the 'Undefined' value instead of having to use an Inner Try/Catch block.

"Rich" wrote:
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
Sep 29 '08 #2
I think you have to use the inner try catch. I may be a bit of a newbie,
but otherwise if you do not use the "inside try catch" by my opinion, your
for next loops will end instantly.

Using the inside for next loops, traps the 1 cell that has errors but allows
the other cells to continue to import.

Miro

"Rich" <Ri**@discussions.microsoft.comwrote in message
news:3E**********************************@microsof t.com...
>I forgot to mention that the Inner Try/Catch did keep the app from
crashing.
So the inner Try/Catch did work! My question is if there is a way to
trap
for the 'Undefined' value instead of having to use an Inner Try/Catch
block.

"Rich" wrote:
>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
Sep 29 '08 #3
Rich wrote:
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.
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
What other properties/methods does the Range object support?
Is there one that would indicate the lack of a value?

From the /little/ I remember of Excel's VBA, doesn't .FormulaR1C1 come
back as an empty string?

HTH,
Phill W.
Sep 30 '08 #4
Not completely sure about .FormulaR1C1

I will have to try it out in Excel and see what I get. Might be an idea.
But you gave me another idea - this may be more an Excel Question than a
vb.net question. Maybe an Excel person has some suggestions. I will have to
try that NG.

Thanks all for your replies.

"Phill W." wrote:
Rich wrote:
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.
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

What other properties/methods does the Range object support?
Is there one that would indicate the lack of a value?

From the /little/ I remember of Excel's VBA, doesn't .FormulaR1C1 come
back as an empty string?

HTH,
Phill W.
Sep 30 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
552
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
8
1797
by: Joona I Palaste | last post by:
We all know that this: void *p; if (p=malloc(1)) { free(p); p; } causes undefined behaviour if malloc() succeeds. But what about this?
66
2992
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
25
3049
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
23
3143
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
2
4417
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
5
1257
by: p_cricket_guy | last post by:
Please see the code below -- start listing is_it_ub.c -- #include <stdio.h> #include <stdlib.h> int main (void) { unsigned char buff;
7
1445
by: thamizh.veriyan | last post by:
Hi, I am new to this community. I have a doubt regarding trap representations. I read in IBM's website that something like this is legal: int main(){ int x=3; {
33
2785
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7044
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7084
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6739
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6929
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.