473,388 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Probably a silly vb.net question

Hi, I've used vb.net for 3 hours(!) now and have probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I have
translated it correctly) in the For... statement.

Thanks in advance.

/Nicke
Jul 21 '05 #1
3 1590
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed it
to "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range
(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.

Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add a
Workbook before the Range method has any meaning. I usedthe following code to get rid of the error that you were
having.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce
-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have

probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I havetranslated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

Jul 21 '05 #2
Keep in mind that, when you use VB.Net to talk to Office,
you are actually interacting with Excel automation using
COM Interop. The cost of crossing the managed/unmanaged
boundry might account for the slowdown you are seeing.
-----Original Message-----
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed it
to "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range
(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.

Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add a
Workbook before the Range method has any meaning. I

used
the following code to get rid of the error that you were
having.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce
-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have

probably
a very easy question. In my VB6 programs I use For Each
Cell in Range... very often which not seems to work in
vb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if Ihavetranslated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

.

Jul 21 '05 #3
OK, I thought it would something like that =). Is there
antoher way to do it?
-----Original Message-----
Keep in mind that, when you use VB.Net to talk to Office,you are actually interacting with Excel automation using
COM Interop. The cost of crossing the managed/unmanaged
boundry might account for the slowdown you are seeing.
-----Original Message-----
Thank you Bruce!

It wasn't the Add (I had that in the original code). It
was "Worksheets.Item(1)" that was missing so I changed itto "For Each Cell In xl.ActiveSheet.Range("A1:C4")" and
it worked... I think I have a little to do before my
program is upgraded to .net...

BTW, I tested a similar code but with a much larger range(5000 rows, 1 column). VB6 did the code in 2.5 sec
and .net in 15 sec!!! I was hoping on a speed enhancment.
Thanks for the help.

/Nicke
-----Original Message-----
I believe the problem is that you need to open or add aWorkbook before the Range method has any meaning. I

used
the following code to get rid of the error that you werehaving.

Dim xl As Excel.Application = New Excel.Application
Dim cell As Excel.Range

xl.Workbooks.Add()
For Each cell In xl.Worksheets.Item(1).Range("A1:C4")
Console.WriteLine("Value: " & cell.Value)
Next

Hope this helps

Bruce Johnson
http://www.ObjectSharp.com/Bruce

-----Original Message-----
Hi, I've used vb.net for 3 hours(!) now and have
probably
a very easy question. In my VB6 programs I use For EachCell in Range... very often which not seems to work invb.net.

Ex.

Dim Cell As Excel.Range
Dim xl as Excel.Application

xl = New Excel.Application

For Each Cell In xl.Range("A1:C4")
What is wrong here? I get "Unknown interface" (if I

have
translated it correctly) in the For... statement.

Thanks in advance.

/Nicke
.

.

.

.

Jul 21 '05 #4

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

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
15
by: Jim | last post by:
This is probably a common question - What is the best CSS editor? I'm an old HTML dinosaur that just getting into CSS. My HTML editor from way back is Homesite. They (Macromedia) tout Topstyle Pro...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
3
by: Nicke | last post by:
Hi, I've used vb.net for 3 hours(!) now and have probably a very easy question. In my VB6 programs I use For Each Cell in Range... very often which not seems to work in vb.net. Ex. Dim Cell...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...

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.