Viewing cells in Excel 97 from VB.net project

Nalco
09-26-2004, 09:21 AM
I am new to VB programming. I have an Excel 97 file that I want to write a VB.net program to view the contents of cell A1 in a VB textbox once I click on a button.

by_m
09-26-2004, 01:33 PM
So whats your question? Do you already have something made or do you have no idea where to start? If you have no idea where to start check this link out:http://www.xtremevbtalk.com/showthread.php?t=160433

Nalco
09-26-2004, 01:54 PM
I know how to do basic forms, command buttons, textboxes. The code I need is what is required to open an Excel 97 file and read a specific cell location and send it to the VB.net textbox.

by_m
09-26-2004, 04:23 PM
The link I gave you in my last post tells how to do the whole excel automation thing. If you have any more questions or problems after looking at that just ask. The thing by Mike R is very well done very well explained so you shouldn't have many (if any) problems.

Nalco
09-26-2004, 04:52 PM
I will try some of the code, but from what I see so far it only write to Excel. I need to know how to open a file and get a cell value and display it. I do not know which methods to use to open an Excel file and then to call a cell. Let me know if you can help.

Thanks :)

by_m
09-26-2004, 04:59 PM
OK I'm going to get some stuff together but could you post some code or just give me a general idea of where you are in your program? Also did you add the project reference yet?

Nalco
09-26-2004, 07:23 PM
I was able to find some code to writ to cells in Excel 97, but i need help on finding code to read the cells. Below is the code I have so far. :)
Public Class Form1
Inherits System.Windows.Forms.Form

Dim Excel As Object
Dim Book As Object
Dim Sheet As Object

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

0:
Excel = CreateObject("Excel.application")
Book = Excel.Workbooks.add

Sheet = Book.Worksheets(1)
Sheet.Range("A1").Value = "A1"
Sheet.Range("A2").Value = "A2"
Sheet.Range("A3").Value = "A3"

Book.SaveAs("C:\Book1.xls")
'Excel.Book.Sheet.Cells(5, 5) = 5000
'Sheet.GetType(Range("A1"))
'txtbx1.Text = Excel.Book.Sheet.cells(1, 1)
'txtbx1.Text = Sheet.Range("A3").Value

'Excel.path("C:\Book1.xls")
'txtbx1.Text = Sheet.Range.Cells(1, 3)
Excel.quit()

I tried some code but it did not work.
'Book.OpenAs("C:\Book1.xls") = True
'Excel.Book.Sheet.Cells(5, 5) = 5000


End Sub

Public Sub bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt1.Click
'Dim range As Excel.Range

'range = Sheet.range("A1")
'txtbx1.Text = Sheet.Range.Cells(1, 1)

Excel.quit()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class

Nalco
09-26-2004, 07:24 PM
The above program will write to a new Excel file you name near the end of the program. It puts values in cell A1, A2, & A3.

by_m
09-26-2004, 07:45 PM
ok cool I see where you are, I had a similar question recently. The code for opening is:

xlapp = CType(CreateObject("Excel.Application"), Excel.Application)
xlWB = CType(xlapp.Workbooks.Add, Excel.Workbook)
xlWB = xlapp.Workbooks.Open(C:\yourfile.xls)
xlapp.Application.Visible = True 'unless you want to keep it in the background

And that should do it

Nalco
09-26-2004, 08:19 PM
Thanks, I was able to open the Excel file that has my data, but I am having trouble finding the right code to read a cell like A1 and put it in my textbox on my VB form. I'm almost there, thanks for your continued help. :D

by_m
09-26-2004, 09:33 PM
Try this:

txtYourTextBox.Text = sheet.Range("A1").Text

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum