Results 1 to 8 of 8

Thread: [RESOLVED] Opening Excel with VB .NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Resolved [RESOLVED] Opening Excel with VB .NET

    Okay I am trying to make a class library that takes information from an excel file and puts it into an access database, but I keep getting this dumb object reference error that always screws me over. Here is my code:

    vb Code:
    1. Dim xlApp As Excel.Application
    2.     Dim xlWbs As Excel.Workbooks
    3.     Dim xlBook As Excel.Workbook
    4.     Dim xlSheet As Excel.Worksheet
    5.     Dim xlRange As Excel.Range
    6.     Dim strExcelPath As New String("C:\Users\Aj\Desktop\Tax_Research_Template_2010_-_Excel.xls")
    7.     Dim accConn As String = "C:\Users\Aj\Desktop\TaxCert_be.accdb"
    8.     Dim xlDT As New DataTable
    9.     Dim xlDR As DataRow = xlDT.NewRow
    10.     Public Sub XLMain()
    11.         xlBook = xlWbs.Open(strExcelPath)
    12.         xlSheet = xlBook.Worksheets(1)
    13.         ReadXL()
    14.         ImportXL()
    15.         CloseExcel()
    16.     End Sub
    Code:
    Object reference not set to an instance of an object.
    xlBook = xlWbs.Open(strExcelPath)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Opening Excel with VB .NET

    You never created xlWbs, so it is Nothing at the time of that call. You might be interested in looking up the Excel class I have over in the CodeBank, as it shows an example of opening these things in a Late Binding fashion, which is advantageous when it comes to Excel. On the other hand, the rest of the class doesn't do anything like what you are doing.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Opening Excel with VB .NET

    vb Code:
    1. Dim xlApp As Excel.Application
    2.     Dim xlWbs As Excel.Workbooks
    3.     Dim xlBook As Excel.Workbook
    4.     Dim xlSheet As Excel.Worksheet
    5.     Dim xlRange As Excel.Range
    6.     Dim strExcelPath As New String("C:\Users\Aj\Desktop\Tax_Research_Template_2010_-_Excel.xls")
    7.     Dim accConn As String = "C:\Users\Aj\Desktop\TaxCert_be.accdb"
    8.     Dim xlDT As New DataTable
    9.     Dim xlDR As DataRow = xlDT.NewRow
    10.     Public Sub XLMain()
    11.         xlWbs = CreateObject("Excel.Application") ' Incorrect, error got thrown here.
    12.         xlBook = xlWbs.Open(strExcelPath)
    13.         xlSheet = xlBook.Worksheets(1)
    14.         ReadXL()
    15.         ImportXL()
    16.         CloseExcel()
    17.     End Sub
    Code:
    Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel.Workbooks'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208DB-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
    I have a feeling what I did wasn't right? I have Dim xlWbs As Excel.Workbooks, and the MSDN suggestion is to put a "new" in there, but it gives me an error saying I can't put that on an "interface"

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Opening Excel with VB .NET

    You create xlApp with CreateObject, not xlWbs. After all, the former is the application, the latter is just a workbook within the application. Actually, I think you don't need xlWbs at all. I think you only need to create the app and open a workbook.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Opening Excel with VB .NET

    vb Code:
    1. Dim xlApp As Excel.Application
    2.     Dim xlWbs As Excel.Workbooks
    3.     Dim xlBook As Excel.Workbook
    4.     Dim xlSheet As Excel.Worksheet
    5.     Dim xlRange As Excel.Range
    6.     Dim strExcelPath As String = "C:\Users\Aj\Desktop\Tax_Research_Template_2010_-_Excel.xls"
    7.     Dim accConn As String = "C:\Users\Aj\Desktop\TaxCert_be.accdb"
    8.     Dim xlDT As New DataTable
    9.     Dim xlDR As DataRow = xlDT.NewRow
    10.     Public Sub XLMain()
    11.         xlApp = CreateObject("Excel.Application")
    12.         xlBook = xlApp.Open(strExcelPath)
    13.         xlSheet = xlBook.Worksheets(1)
    14.         'Now Excel is open, do stuff
    15.         ReadXL()
    16.         ImportXL()
    17.         'Close Excel:
    18.         CloseExcel()  'Call Close Excel Sub
    19.     End Sub
    Code:
    Public member 'Open' on type 'ApplicationClass' not found.
    xlBook = xlApp.Open(strExcelPath)
    How do I get xlBook as the "book" and open it so that I can set xlSheet as the sheet, I have googled a lot but all of the examples that I find and try, end up not working and giving me object reference errors. I even used an example from a codeguru article that failed me. Basically, I only need the xls file open to do this:

    vb Code:
    1. Try
    2.             ordernumber = xlSheet.Cells(5, "C")
    3.             owner1 = xlSheet.Cells(6, "C")
    4.             county = xlSheet.Cells(6, "I")
    5.             fulladdress = xlSheet.Cells(7, "C")
    6.             address = Mid(fulladdress, 0, InStr(fulladdress, ",") - 1)
    7.             zip = Mid(fulladdress, fulladdress.Length - 5, 5)
    8.             city = Mid(fulladdress, address.Length + 2, InStr(fulladdress, ",") - 1)
    9.             state = Mid(fulladdress, address.Length + city.Length + 4, 2)
    10.         Catch ex As Exception
    11.             MsgBox(ex.ToString)
    12.         End Try

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Opening Excel with VB .NET

    That much can all be found in the Excel class I have over in the CodeBank. Everything is type Object, but the names should pretty well explain what the variable holds.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Opening Excel with VB .NET

    vb Code:
    1. Protected Function OpenApp() As Boolean
    2.         'Create the app.
    3.         Try
    4.             If xlApp Is Nothing Then
    5.                 xlApp = CreateObject("Excel.Application")
    6.             End If
    7.             xlApp.AlertBeforeOverwriting = False
    8.             xlApp.DisplayAlerts = False
    9.             ImOpen = True
    10.             GetVersion()
    11.         Catch ex As Exception
    12.             If mVerbose Then
    13.                 Windows.Forms.MessageBox.Show(ex.Message, "Failed to Open Excel", MessageBoxButtons.OK, MessageBoxIcon.Information)
    14.             End If
    15.             ImOpen = False
    16.         End Try
    17.  
    18.         OpenApp = ImOpen
    19.     End Function
    Code:
    xlBook = xlApp.Workbooks.Open(mBookName, ReadOnly:=False, Notify:=False)
    These are the only 2 things I see in there that have anything to do with opening the excel file to read from it, if it was somewhere else I must have missed it. Where do you set the location of the excel file?
    vb Code:
    1. xlBook = xlApp.Workbooks.Open(strExcelPath)
    2.         xlSheet = xlBook.Worksheets(1)
    I don't get an error with this until I get to
    Code:
    ordernumber = xlSheet.Cells(5, "C").ToString
    and from there I get an error saying
    Code:
    Conversion from type 'Range' to type 'String' is not valid.
    I turn options Strict On and I am forced to change everything so that it looks like this
    vb Code:
    1. xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
    2.         xlBook = xlApp.Workbooks.Open(strExcelPath)
    3.         xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
    and I try this
    Code:
    ordernumber = CStr(xlSheet.Cells(5, "C"))
    And I still get this
    Code:
    Conversion from type 'Range' to type 'String' is not valid.
    Is excel even opening? I have about 8 EXCEL.EXE *32 running in my process list but they don't seem to be taking any CPU or RAM.
    Last edited by thebuffalo; May 23rd, 2012 at 08:59 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Opening Excel with VB .NET

    vb Code:
    1. Public Sub XLMain()
    2.         xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
    3.         xlBook = xlApp.Workbooks.Open(strExcelPath)
    4.         xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
    5.         'Now Excel is open, do stuff
    6.         ReadXL()
    7.         ImportXL()
    8.         'Close Excel:
    9.         CloseExcel()  'Call Close Excel Sub
    10.     End Sub
    vb Code:
    1. ordernumber = xlSheet.Cells(5, "C").Value.ToString
    2.             owner1 = xlSheet.Cells(6, "C").Value.ToString
    3.             county = xlSheet.Cells(6, "I").Value.ToString
    4.             fulladdress = xlSheet.Cells(7, "C").Value.ToString
    Works. Thanks.

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