Exception HRESULT: 0x800401A8 in Excel with VB .Net

G

Guest

Hello
I'm making an application that uses an excel worksheet, My problem appears
when I open the worksheet to see the data (then I close it and save the
changes) and I try to insert a new row from my application appears the error:

System.Runtime.InteropServices.COMException(0x800401A8): Exception
HRESULT: 0x800401A8

Thanks for all
 
G

Guest

sgr,

You said your steps are:

1. open workbook
2. read data
3. close workbook
4. save workbook
5. edit workbook

My guess is that you are getting that error because the workbook you
attempting to change isn't open.

Why don't you post some code? That would let us know exactly what you are
trying to do.

Thanks,

Kim Greenlee
 
G

Guest

Thanks Kim,
I opened the taksmanager I saw that the process excel.exe was always
running, although I close the application.

This is my code:

Dim xlsApp As Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Dim xlsRange As Excel.Range
Dim filexls As String = "c:\temp\temp.xls"

Private EventDel_BeforeBookClose As
Excel.AppEvents_WorkbookBeforeCloseEventHandler


Private Sub BeforeBookClose(ByVal Wb As Excel.Workbook, ByRef Cancel As
Boolean)
closeXls()
RemoveHandler xlsApp.WorkbookBeforeClose, EventDel_BeforeBookClose
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'open excel, edit some cells and close it

xlsApp = CreateObject("Excel.Application")
xlsBook = xlsApp.Workbooks.Add
xlsBook = xlsApp.Workbooks.Open(filexls)
xlsSheet = xlsBook.Worksheets(1)

EventDel_BeforeBookClose = New
Excel.AppEvents_WorkbookBeforeCloseEventHandler(AddressOf BeforeBookClose)
AddHandler xlsApp.WorkbookBeforeClose, EventDel_BeforeBookClose

xlsApp.Visible = True

End Sub

Public Sub closeXls()
xlsBook.Save()
xlsRange = Nothing
xlsSheet = Nothing
xlsBook.Close(False)
xlsBook = Nothing
xlsApp.Quit()
xlsApp = Nothing
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
xlsApp = CreateObject("Excel.Application")
xlsBook = xlsApp.Workbooks.Add
xlsBook = xlsApp.Workbooks.Open(filexls)
xlsSheet = xlsBook.Worksheets(1)

xlsSheet.Columns.Clear()

EventDel_BeforeBookClose = New
Excel.AppEvents_WorkbookBeforeCloseEventHandler(AddressOf BeforeBookClose)
AddHandler xlsApp.WorkbookBeforeClose, EventDel_BeforeBookClose

xlsSheet.Range("A1:L1").Font.Bold = True
xlsSheet.Range("A1:L1").Font.Name = "Verdana"
xlsSheet.Range("A1:L1").Borders.Weight = Excel.XlBorderWeight.xlMedium
xlsRange = xlsSheet.Range("A1")
xlsRange.Value = "Hello"
xlsRange = xlsSheet.Range("B1")
xlsRange.Value = "5.71"
xlsRange = xlsSheet.Range("C1")
xlsRange.Value = "9.02"

xlsRange = xlsSheet.Range("A3")
xlsRange.Value = TextBox1.Text

xlsBook.Save()

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top