Results 1 to 10 of 10

Thread: [RESOLVED] [2008] Retrieve value from excel file

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    Resolved [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.

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    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?

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    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?

  5. #5
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    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

  7. #7
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    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.

  9. #9
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    7

    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
  •  



Click Here to Expand Forum to Full Width