Need help reading multiple worksheets in a single excel workbook

dugald
10-06-2004, 12:08 PM
I'm using VB.NET 2003 with Excel 2002 SP3. I am trying to generate some code that will read Values from 3 different worksheets in a single workbook and post the values in 3 different text boxes. Seemed easy enough at face value. The code I have is:

Dim studentApp As New Excel.Application
Dim studentWB As Excel.Workbook = studentApp.Workbooks.Open("E:\major2.xls")
Dim xWS As Excel.Worksheet = CType(studentWB.Worksheets("flowchart"), Excel.Worksheet)
Dim yWS As Excel.Worksheet = CType(studentWB.Worksheets("completed"), Excel.Worksheet)
Dim zWS As Excel.Worksheet = CType(studentWB.Worksheets("current"), Excel.Worksheet)

Dim s As String

xWS.Range("A2:C10").Select()
Dim xRange As Excel.Range = studentApp.Selection()
For i As Integer = 1 To 10
For j As Integer = 1 To 3
s = xRange(i, j).Value
TextBox1.Text &= s & vbTab
Next
TextBox1.Text = TextBox1.Text & vbCrLf
Next

'This is done 3 times (once for each worksheet and workbook)
studentWB.Close(SaveChanges:=False)
studentApp.Quit()

studentApp = Nothing
studentWB = Nothing
xWS = Nothing
yWS = Nothing
zWS = Nothing

GC.Collect()

My problem is when it's getting to the lines "yWS.Range("A2:C10").Select()" or "zWS.Range("A2:C10").Select()" the program crashes and I get the error message:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Select method of Range class failed

I've been digging through various webpages for the last week looking for a solution. Anything would be greatly appreciated.

Dugald

Mike Rosenblum
10-06-2004, 12:36 PM
Try to avoid using .Select and .Selection at all costs. If you need to highlight something for the User, then I guess it's ok, but otherwise, never, ever use it. So I would change your code from:xWS.Range("A2:C10").Select()
Dim xRange As Excel.Range = studentApp.Selection() to:
Dim xRange As Excel.Range = xWS.Range("A2:C10) As for why your code failed, I would think that this is casting issue. Excel.Application.Selection() can return a Range or a Chart or a Control -- anything that might be currently highlighted. If you were "hell-bent" on using the Select/Selection methodology, then a CType() would probably get you through:xWS.Range("A2:C10").Select()
Dim xRange As Excel.Range = CType(studentApp.Selection(), Excel.Range) If you add 'Option Strict On' to the top of your module, it would pick up problems like this right away. :)

Hope this helps!
Mike

dugald
10-06-2004, 12:46 PM
Works great. Thanks for the help. I've never used excel with VB.NET before so I'm still learning exactly how to declare things like this. Thanks again for the help.

Mike Rosenblum
10-06-2004, 12:51 PM
Wow, that's a first... I don't think I've ever seen my advice actually work off the first go before! :)

Yeah, .Net is tricky with COM Applications like Excel. I would definately put 'Option Strict On' at the top of your Module. That way the Compiler will tell you directly where you need a CType().

If you haven't given the Automating Office Programs with VB.Net (http://www.xtremevbtalk.com/showthread.php?t=160433) tutorial a read, you might want to. It's pretty basic (that is I think you're probably past its level), but you might find something in there of value to you...

But, in short, welcome to the Forum and I'm glad you're on your feet! :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum