Can't close Excel process

patrickoq

New member
Joined
Oct 31, 2010
Messages
1
Programming Experience
Beginner
HEy Guys,

Maybe you can help with my problem. I know this subject, has been posted hundreds times, but have tried everything and still can't solve my problem.

So, let me give you more details. In the code below, I open excel file, when I try to enter some values into excel cells, and close it afterwards. For some reasons excel process is still active in task manager. I am releasing objects, quiting application like for other excel files in my program, but in this specific example can't do this.

The code below is written in Visual Studio 2010, windows 7. Hopefuly, you find something what I don't see after a few hours spent with it :)

Thanks in advance for your suggestions. And sorry if I did something, it's my first post here :)

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

Imports Microsoft.Office.Interop
Public Class Form1
Public MainPath As String = System.IO.Directory.GetCurrentDirectory()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim pathTOFile As String


pathTOFile = MainPath & "\output\" & Format(Today, "MMM d yyyy") & "\"
pathTOFile = pathTOFile & "Auto.xlsx"

xlWorkBook = xlApp.Workbooks.Open(pathTOFile)
xlWorkSheet = xlWorkBook.ActiveSheet

'What is interesting. If I comment all xlWorkSheet.Cells lines except first 2, the 'process is stopped
xlWorkSheet.Cells(1, 1) = "test1"
xlWorkSheet.Cells(1, 2) = "test2"
xlWorkSheet.Cells(1, 3) = "test3"
xlWorkSheet.Cells(1, 4) = "test4"
xlWorkSheet.Cells(1, 5) = "test3"
xlWorkSheet.Cells(1, 6) = "test3"
xlWorkSheet.Cells(1, 7) = "test3"
xlWorkSheet.Cells(1, 7) = "test3"
xlWorkSheet.Cells(1, 8) = "test3"
xlWorkSheet.Cells(1, 9) = "test3"
xlWorkSheet.Cells(1, 10) = "test3"



NA(xlWorkSheet)
xlApp.DisplayAlerts = False
xlWorkBook.Close(True, pathTOFile)
xlApp.DisplayAlerts = True
NA(xlWorkBook)
xlApp.Workbooks.Close()
NA(xlApp.Workbooks)
xlApp.Quit()
NA(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
System.Threading.Thread.Sleep(5000)


End Sub

Private Sub NA(ByVal ooo As Object)
Try
While (System.Runtime.InteropServices.Marshal.ReleaseComObject(ooo) <> 0)

End While
Catch
Finally
ooo = Nothing
End Try
End Sub

End Class
 
Back
Top