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
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