title: Application not quitting after calling quit
body: <p>I have a small problem that I can't seem to figure out. I am saving a DataGridView (it's contents) to an xls file. I have no problem in doing so except in my task manager its still showing up that it's running. I have called:</p>
  
  <pre><code>  xlApp.Application.Quit() 
  </code></pre>
  
  <p>This is declared as:      </p>
  
  <pre><code>  Dim xlApp As New excel.Application
  </code></pre>
  
  <p>This seems to not work, BUT this is the same way I quit when I let the user choose to export it to a Word Document. Im not sure where I am going wrong...</p>
  
  <p>Here is my complete code</p>
  
  <pre><code>Imports Word = Microsoft.Office.Interop.Word
   Imports Excel = Microsoft.Office.Interop.Excel
  
   Public Class Form1
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      For x As Integer = 1 To 3500
          DataGridView1.Rows.Add(New Object() {"r" &amp; x.ToString &amp; "c1", "r" &amp; x.ToString &amp; "c2", "r" &amp; x.ToString &amp; "c3", "r" &amp; x.ToString &amp; "c4", "r" &amp; x.ToString &amp; "c5"})
      Next
  End Sub
  
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      exportToWord (DataGridView1)
  End Sub
  
  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 misValue As Object = System.Reflection.Missing.Value
  
  
      xlWorkBook = xlApp.Workbooks.Add
      xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
  
      xlApp.Visible = True
  
      Dim headers = (From ch In DataGridView1.Columns _
                    Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
                    Select header.Value).ToArray()
      Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
  
      Dim items() = (From r In DataGridView1.Rows _
            Let row = DirectCast(r, DataGridViewRow) _
            Where Not row.IsNewRow _
            Select (From cell In row.Cells _
                Let c = DirectCast(cell, DataGridViewCell) _
                Select c.Value).ToArray()).ToArray()
  
      Dim table As String = String.Join(vbTab, headerText) &amp; Environment.NewLine
      For Each a In items
          Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
          table &amp;= String.Join(vbTab, t) &amp; Environment.NewLine
      Next
      table = table.TrimEnd(CChar(Environment.NewLine))
      Clipboard.SetText (table)
  
      Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
  
      Dim range As excel.Range = xlWorkSheet.Range("B2:" &amp; alphabet(headerText.Length) &amp; (items.Length + 2).ToString)
  
      range.Select()
      xlWorkSheet.Paste()
  
      range.Borders(Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone
      range.Borders(Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone
      With range.Borders(Excel.XlBordersIndex.xlEdgeLeft)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With range.Borders(Excel.XlBordersIndex.xlEdgeTop)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With range.Borders(Excel.XlBordersIndex.xlEdgeBottom)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With range.Borders(Excel.XlBordersIndex.xlEdgeRight)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With range.Borders(Excel.XlBordersIndex.xlInsideVertical)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlThin
      End With
      With range.Borders(Excel.XlBordersIndex.xlInsideHorizontal)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlThin
      End With
  
      'xlApp.Visible = True
  
      xlWorkBook.SaveAs("C:\Users\CoDeXeR\Desktop\Word1.xls", True)
      xlWorkBook.Close()
      xlApp.Application.Quit()
  
      ReleaseObject(xlWorkSheet) '&lt;~~~ Added as per comment from deleted post
      ReleaseObject (xlWorkBook)
      ReleaseObject (xlApp)
  
  
  End Sub
  
  Public Sub exportToWord(ByVal dgv As DataGridView)
      ' Create Word Application
      Dim oWord As Word.Application = DirectCast(CreateObject("Word.Application"), Word.Application)
  
      ' Create new word document
      Dim oDoc As Word.Document = oWord.Documents.Add()
  
  
      Dim headers = (From ch In dgv.Columns _
                    Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
                    Select header.Value).ToArray()
      Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
  
      Dim items() = (From r In dgv.Rows _
            Let row = DirectCast(r, DataGridViewRow) _
            Where Not row.IsNewRow _
            Select (From cell In row.Cells _
                Let c = DirectCast(cell, DataGridViewCell) _
                Select c.Value).ToArray()).ToArray()
  
      Dim table As String = String.Join(vbTab, headerText) &amp; Environment.NewLine
      For Each a In items
          Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
          table &amp;= String.Join(vbTab, t) &amp; Environment.NewLine
      Next
      table = table.TrimEnd(CChar(Environment.NewLine))
      Clipboard.SetText (table)
  
      Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, items.Count + 1, headers.Count)
  
      oTable.Range.Paste()
  
      'make the first row bold, fs 14 + change textcolor
      oTable.Rows.Item(1).range.Font.Bold = &amp;H98967E
      oTable.Rows.Item(1).range.Font.Size = 14
      oTable.Rows.Item(1).range.Font.Color = Word.WdColor.wdColorWhite
  
      'change backcolor of first row
      oTable.Rows.Item(1).range.Shading.Texture = Word.WdTextureIndex.wdTextureNone
      oTable.Rows.Item(1).range.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic
      oTable.Rows.Item(1).range.Shading.BackgroundPatternColor = Word.WdColor.wdColorLightBlue
  
      ''set table borders
      'With oTable.Range.Tables(1)
      '    With .Borders(Word.WdBorderType.wdBorderLeft)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth100pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    With .Borders(Word.WdBorderType.wdBorderRight)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth100pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    With .Borders(Word.WdBorderType.wdBorderTop)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth100pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    With .Borders(Word.WdBorderType.wdBorderBottom)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth100pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    With .Borders(Word.WdBorderType.wdBorderHorizontal)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth050pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    With .Borders(Word.WdBorderType.wdBorderVertical)
      '        .LineStyle = Word.WdLineStyle.wdLineStyleSingle
      '        .LineWidth = Word.WdLineWidth.wdLineWidth050pt
      '        .Color = Word.WdColor.wdColorAutomatic
      '    End With
      '    .Borders(Word.WdBorderType.wdBorderDiagonalDown).LineStyle = Word.WdLineStyle.wdLineStyleNone
      '    .Borders(Word.WdBorderType.wdBorderDiagonalUp).LineStyle = Word.WdLineStyle.wdLineStyleNone
      '    .Borders.Shadow = False
      'End With
      ' Save this word document
      oDoc.SaveAs("C:\Users\CoDeXeR\Desktop\Word1.doc", True)
      oDoc.Close()
      oWord.Application.Quit()
      'oWord.Visible = True
  
  End Sub
  
  Public Sub exportToExcel(ByVal dgv As DataGridView)
  
  End Sub
  
  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
  
   End Class
  </code></pre>