|
-
Sep 24th, 2008, 04:46 PM
#1
Thread Starter
New Member
[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
Last edited by Roxbury22; Sep 24th, 2008 at 05:46 PM.
-
Sep 25th, 2008, 01:18 AM
#2
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
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Sep 25th, 2008, 10:42 AM
#3
Thread Starter
New Member
Re: [2008] Retrieve value from excel file
 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?
-
Sep 25th, 2008, 02:38 PM
#4
Thread Starter
New Member
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?
-
Sep 25th, 2008, 04:01 PM
#5
Addicted Member
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)
VB version: Visual Studio 2008-Professional
-
Sep 25th, 2008, 04:46 PM
#6
Thread Starter
New Member
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
-
Sep 26th, 2008, 08:47 AM
#7
Addicted Member
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.
VB version: Visual Studio 2008-Professional
-
Sep 26th, 2008, 02:49 PM
#8
Thread Starter
New Member
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.
-
Sep 26th, 2008, 03:04 PM
#9
Addicted Member
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)
VB version: Visual Studio 2008-Professional
-
Sep 26th, 2008, 03:22 PM
#10
Thread Starter
New Member
Re: [2008] Retrieve value from excel file
It is not listed in the .NET tab.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|