[RESOLVED] [2008] Retrieve value from excel file
Hello, I am trying to read an excel 2007 file. All I want is the first box in the excel file to be dispayed in a label on my windows form. I found a tutorial on here, however, it is not working. Seems like an easy thing to do, any suggestions? I am using VB .Net 2008 and Excel 2007. I get an exception error on the line where I declare the file name of the excel file.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oXLApp As Excel.Application
Dim oXLBook As Excel.Workbook
Dim oXLSheet As Excel.Worksheet
oXLBook = oXLApp.Workbooks.Open(Application.StartupPath & "Book1.xlsx")
oXLApp.Visible = True
oXLBook = Nothing
oXLApp = Nothing
Dim my_variable As String
my_variable = oXLSheet.Cells(1, 1).Value
Label1.Text = "Current: " & my_variable
End Sub
Re: [2008] Retrieve value from excel file
You need to initiate the applacation as well
XlApp contains the value Nothing
Dim oXLApp As New Excel.Application
would help probably
Re: [2008] Retrieve value from excel file
Quote:
Originally Posted by Dnereb
You need to initiate the applacation as well
XlApp contains the value Nothing
Dim oXLApp As New Excel.Application
would help probably
When I put New in there, it gives me an error. "New canot be used on an interface."
Is there an easier way to pull one cell from excel?
Re: [2008] Retrieve value from excel file
Does anyone have an example of how to pull the value of just one cell from an excel file?
Re: [2008] Retrieve value from excel file
Add at top of project:
Code:
Imports Microsoft.office.interop
then this is a snippet from one of my programs where i am writting to an excel file. Try to understand this and reverse it.
Code:
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
oApp = New Excel.Application
oWB.Sheets("Sheet1").Cells(i + 1, 1).Value = PrePressname(i)
oWB.Sheets("Sheet1").Cells(i + 1, 10).Value = PrePressPrice(i)
Re: [2008] Retrieve value from excel file
OK, I'm recieving the same error when I add the "New". It says "New cannot be used on an interface". I've reworked the code to fit what you posted, here is the entire thing. I tried to google the error and it mentions adding an excel reference, do you have any ideas? I'm using Excel 2007.
Code:
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
oApp = New Excel.Application
oWB = oApp.Workbooks.Open(Application.StartupPath & "Book1.xlsx")
Label1.Text = "Current: " & oWB.Sheets("Sheet1").Cells(1, 1).Value()
End Sub
End Class
Re: [2008] Retrieve value from excel file
I forget what one exactly it is to add a reference to, maybe someone else here can be of assistance.
How to do it:
Go to project on menu bar----> Add a reference
And i believe if you go to the COM tab and scroll down to the Microsoft Excel 11.0 Library this will solve that problem.
Re: [2008] Retrieve value from excel file
I tried that already and it did not work. My COM tab shows Excel 12.0 though, not 11.0 if that matters.
Re: [2008] Retrieve value from excel file
Try adding the interop as well from the .NET tab.
I cant remember what did it form me. Im at work trying to guess blindly here (dont have access to it here)
Re: [2008] Retrieve value from excel file
It is not listed in the .NET tab.