Problem with MS Excel

rahavtom

Well-known member
Joined
Apr 2, 2004
Messages
63
Hello !

I have a procedure which export data to MS Excel file and rub macro.
I call this procedure once and it works perfect, but when I try to call it again during the same programs run the application freezes.
I use the followin code:

Public Sub ExportToExcel()

Dim oExcel As Excel.ApplicationClass
Dim oBook As Excel.WorkbookClass
Dim oBooks As Excel.Workbooks
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oBooks = oExcel.Workbooks
oBook = oBooks.Open("c:\temp\graphs.xls")

Dim excelWorksheet As Excel.Worksheet = CType(oBook.Worksheets(1), Excel.Worksheet)

excelWorksheet.Range("A1").Value = 80
oExcel.Run("PrintGraphs")
oBook.Close(False)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
oBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
oBooks = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
GC.Collect()

End Sub

Can someone explain me why this procedure runs only once, and what I should change?

Thanks!
Tom.
 
Back
Top