Results 1 to 7 of 7

Thread: Excel Object Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    14

    Excel Object Error

    Here is the code:
    1 Dim exExcel As Excel.Application
    2 Dim exWorkbook As Excel.Workbook
    3 Dim exSheet As Excel.Worksheet

    5 exExcel = New Excel.Application
    6 exWorkbook = exExcel.Workbooks.Open(strFilename)
    7 exSheet = exWorkbook.Worksheets("Sheet1")



    I get an error at line 6 saying:
    ---------------------------------------------------------------------------
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Program.exe

    Additional information: The server threw an exception.

    ---------------------------------------------------------------------------

    Now I know the file exists, and all dll's are loaded. Why am I coming up with this error?

    Just in case, it might help to know that I am running under the NET framework, not vb6. I am using Excel 8.0 Object Library
    If you own a Compaq, throw it out. They are ghay!

  2. #2
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    i dont know about how u do this in NET framework
    but in vb you should use the SET keyword to reference an object

    Code:
     Dim exExcel As Excel.Application
     Dim exWorkbook As Excel.Workbook
     Dim exSheet As Excel.Worksheet
    
     Set exExcel = New Excel.Application
     Set exWorkbook = exExcel.Workbooks.Open(strFilename)
     Set exSheet = exWorkbook.Worksheets("Sheet1")
    live, code and die...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    14

    Solved!

    Actually I solved the problem myself, but I had to do quite a bit of searching to get it. Here is the update code:

    Option Strict Off

    Public Shared xlApp As Object
    Public Shared xlBook As Object
    Public Shared xlSheet As Object

    xlApp = CreateObject("Excel.Application")
    'Late bind an instance of an Excel workbook.
    xlBook = xlApp.Workbooks.Add
    'Late bind an instance of an Excel worksheet.
    xlSheet = xlBook.Worksheets(1)
    xlSheet.Activate()

    xlBook.SaveAs(strFilename)
    If you own a Compaq, throw it out. They are ghay!

  4. #4
    Hyperactive Member csar's Avatar
    Join Date
    Mar 2002
    Location
    Siam
    Posts
    288
    Option Strict Off

    Public Shared xlApp As Object
    Public Shared xlBook As Object
    Public Shared xlSheet As Object

    xlApp = CreateObject("Excel.Application")
    'Late bind an instance of an Excel workbook.
    xlBook = xlApp.Workbooks.Add
    'Late bind an instance of an Excel worksheet.
    xlSheet = xlBook.Worksheets(1)
    xlSheet.Activate()

    xlBook.SaveAs(strFilename)
    your code for VB6 or .Net I never use Shared statement
    Don't leave it till tomorrow, Do It Now!
    5361726176757468204368616E63686F747361746869656E

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    14
    i don't see the point of not using shared, whats so bad about it?
    If you own a Compaq, throw it out. They are ghay!

  6. #6
    Hyperactive Member csar's Avatar
    Join Date
    Mar 2002
    Location
    Siam
    Posts
    288
    Sorry for my english..

    I mean, I don't know about Shared statement. It's used for VB6 or VB.Net.


    I have same problem with u, how to solve please?
    Don't leave it till tomorrow, Do It Now!
    5361726176757468204368616E63686F747361746869656E

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2003
    Posts
    14
    The .NET code looks like this:

    Dim strConn As String
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strFilename & ";" & _
    "Extended Properties=Excel 8.0;"
    'Excel Sheet Name as TableName (Make sure where Sheet1 appears that you change the name to your sheet name)
    xlOleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
    xlOleDbDataAdapter.Fill(DataForm.xlData, "Sheet1")
    xlGrid.DataSource = DataForm.xlData.Tables(0).DefaultView
    If you own a Compaq, throw it out. They are ghay!

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