Open Excel file

MikeLI

Member
Joined
Dec 28, 2005
Messages
8
Programming Experience
Beginner
Hi All,

When I open an Excel file, I got the following runtime error:
---------------------
System.Runtime.InteropServices.COMException was unhandled ErrorCode=-2147221164
Message="Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154."
---------------------

My code is listed below (the error comes from the highlighted statement, please tell me the reason):
<===========================
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
Public fname As String
Public inApp As Excel.Application
Public inBook As Excel.Workbook

Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
inApp = New Excel.Application
inBook = New inApp.Workbook
fname = txtInFile.Text + ".xls"
inBook.Open(Filename:=fname)
End Sub

Private Sub btnCloseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseFile.Click
inBook.Close()
inBook = Nothing
inApp.Quit()
inApp = Nothing
End Sub
End Class
============================>

I use Office 2003, so, I've added an Excel 11.0 object library and Office 11.0 object library.

Mike
 
I've solved the problem.
Code is listed below if you are interested:
VB.NET:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
  Public fname As String
  Public inApp As Excel.Application
  Public inBook As Excel.Workbook
 
  Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
    inApp = New Excel.Application
    inApp.Visible = True
    fname = txtInFile.Text + ".xls"
[COLOR=red]   inBook = inApp.Workbooks.Open(fname)[/COLOR] 
  End Sub
 
  Private Sub btnCloseFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseFile.Click
    inBook.Close()
    inBook = Nothing
    inApp.Quit()
    inApp = Nothing
  End Sub
End Class
 
Back
Top