1

I'm trying to write some data from my windowsform into a Excel file, this works.

        ' Excel load data
    Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
    Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

    oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
    oWorkSheet = oWorkBook.Worksheets(1)

    oWorkSheet.Range("A1").Value = "Test"

    oWorkBook.Save()
    oWorkBook.Close()

Problem is: When I am done Excel is still running in my task manager. When I press the button like 10 times there are 10 Excel references in my task manager.

Question: How can I fully unload Excel after writing the value into the Excel?

Trevor
  • 7,777
  • 6
  • 31
  • 50
JefE
  • 137
  • 1
  • 2
  • 12
  • There is already an answer here: **http://stackoverflow.com/questions/15697282/excel-application-not-quitting-after-calling-quit**. Please check it out it has great information pertaining why this happens, check it out. – Trevor Sep 04 '15 at 14:06

2 Answers2

2

You need to Quit Microsoft Excel and then release the objects.

Code referenced from this answer: The proper way to dispose Excel com object using VB.NET? and Excel application not quitting after calling quit

'Excel load data
Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim oWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

oWorkBook = oExcelApp.Workbooks.Open("C:\Temp\Test.xlsx")
oWorkSheet = oWorkBook.Worksheets(1)

oWorkSheet.Range("A1").Value = "Test"

oWorkBook.Save()
oWorkBook.Close()

oExcelApp.Quit()

'Release object references.
releaseObject(oWorkSheet)
releaseObject(oWorkBook)
releaseObject(oExcelApp)

----------------------------------------------------------------------------
Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub
Community
  • 1
  • 1
ManishChristian
  • 3,759
  • 3
  • 22
  • 50
  • Could you please explain in your answer **why this happens and why you need to call `ReleaseComObject`** that way other people know what causes this issue. – Trevor Sep 04 '15 at 14:12
  • Take a look at [this](http://stackoverflow.com/questions/15697282/excel-application-not-quitting-after-calling-quit) and [this](http://stackoverflow.com/questions/10309365/the-proper-way-to-dispose-excel-com-object-using-vb-net) questions. – ManishChristian Sep 04 '15 at 14:25
  • I know where to look it was my question :) I asked you to explain. If you can't `please cite your sources!` – Trevor Sep 04 '15 at 14:27
  • Thanks for all great advice, and references. Now onto the next hurdle ;) – JefE Sep 08 '15 at 07:55
0

You have to close the connection you just opened. You can do this by adding this line after your current code:

oExcelApp.Quit();

Source & more information: here

More information about the quit() method here.

Important: if you have open workbooks that are not saved yet, this method will show a dialog box asking to save.

If you don't want this, you either have to (1): save all open workbooks or (2) setting DisplayAlerts to false.

(1)

workbooks.Save()

(2)

oExcelApp.DisplayAlerts = false
Jordumus
  • 2,763
  • 2
  • 21
  • 38