Newbie - Read an excel cell's content

  • Thread starter Thread starter Kay
  • Start date Start date
K

Kay

Hello,

I'm trying to read an excel's and display it in a message box- I think it's
pretty forward (at least in vb6) but I've been spent hours still can't
figure it out.

The message box always display "System.__ComObject", can you please tell me
what's wrong?

=========
Dim oApp As New Excel.Application
Dim oWBa As Excel.Workbook = oApp.Workbooks.Open("c:\KayTest.XLS")
Dim oWSa As Excel.Worksheet

oApp.Visible = False
oWSa = oWBa.Worksheets(1)

MsgBox(oWSa.Cells("2", "b").ToString)

oWBa.Close(False)
oApp.Quit()
oApp = Nothing
oWBa = Nothing
oWSa = Nothing
GC.Collect()
==========

Thanks!

Kay.
 
Kay,

Cells(R,C)
Not Column, Row

in your example Cells(2,2)

C2 would be Cells(2,3)

Doug
 
Hi Doug,

Thanks for your respond~

I tried :

MsgBox(oWSa.Cells("2", "3"))
or
MsgBox(oWSa.Cells(2, 3))

But both doesn't work, am I missing something obvious?

Thanks~

Kay.
 
Excel is certainly much harder to drive from .Net than VB

Imports System.IO

Imports System.Reflection ' For Missing.Value and BindingFlags

Imports System.Runtime.InteropServices ' For COMException



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim exApp As New Excel.Application

Dim exWBa As Excel.Workbook

Dim exWSa As Excel.Worksheet

Dim exRng As Excel.Range

Dim stFile, stVal As String

Dim fil As File

exApp.Visible = True

stFile = "C:\Documents and Settings\DacTech\My
Documents\DacTech\_DotNetSamples\Test\Test.xls"

If fil.Exists(stFile) Then

exWBa = exApp.Workbooks.Open(stFile)

exWSa = CType(exWBa.Worksheets(1), Excel.Worksheet)

exWSa.Activate()

exRng = exWSa.Range("B1", Missing.Value)

Dim args1(1) As [Object]

args1(0) = 1

stVal = exRng.Value.ToString()

MsgBox(stVal)

End If

exWBa.Close(False)

exApp.Quit()

End Sub

Doug
 
Back
Top