views:

987

answers:

2

I've tried this:

    'start Excel app
    Dim exApp As Microsoft.Office.Interop.Excel.Application
    exApp = CreateObject("Excel.Application")

    ' load excel document
    exApp.Workbooks.Open(fname)
    Dim exSheet As Microsoft.Office.Interop.Excel.Worksheet
    exSheet = exApp.Workbooks(1).Worksheets(1)

and than, for example accessing "C3" cell:

 Dim b As String
 b = exSheet.Cells("A3")

or:

b = exSheet.Cells(3,3)

and it throws me an exception. I'm feeling that I'm doing something wrong with the object access, but this method worked in embedded VB, and do not works in .net. Also, tried to google the exception code, with no relevant result.

+1  A: 

I don't think you should write code in VB6 style for vb.net.

Looking at the code example, I think what you need is

b = exSheet.Cells(3,3).Text

or

b = exSheet.Cells(3,3).Value

EDIT: I guess it the reference should be assigned to an instance of range.
So, the code might look like

Range exampleRange = exSheet.Cells(3,3)
b = exampleRange.Text 'OR it can be b = exampleRange.Value
shahkalpesh
Intellisense shows me only ToString, Equals, GetHashCode, GetType and ReferenceEquals. Should I use one of them?
A: 

Try:

b = exSheet.Range("A3").Value.ToString
Mark
Thanks, this works fine.Do Range method get another parameters, like (x,y) position to iterate through spreadsheet?
Hmm, you can use .Cells, but you would need exSheet.Cells()(3,3).Value2
Mark