title: Visual Basics not closing excel when range is used
body: <p>In a previous post (<a href="https://stackoverflow.com/questions/15697282/excel-application-not-quitting-after-calling-quit">Excel application not quitting after calling quit</a>) there's a "tried and tested" method of doing this.</p>
  
  <p>I have a problem with this, in that, as it is written, it doesn't work for me. </p>
  
  <p>VB 2010, Excel 2010</p>
  
  <p>My Excel instance is <em>not</em> clearing out of the task manager when I release the Object. However, if I comment out <em>all</em> references to range, it works. </p>
  
  <p>Why is RANGE messing it up, and how do I solve it?</p>
  
  <pre><code>Imports Microsoft.Office.Interop
  Module Module1
  
      Sub Main()
      Dim xlApp As New excel.Application
      Dim xlWorkBook As excel.Workbook
      Dim xlWorkSheet As excel.Worksheet
      Dim xlRange As excel.Range
      'Dim misValue As Object = System.Reflection.Missing.Value
      xlApp.DisplayAlerts = False
      xlWorkBook = xlApp.Workbooks.Add
      xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
  
      xlApp.Visible = True
  
  
  
      Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
  
      xlRange = xlWorkSheet.Range("B2:C23")
  
      xlRange.Select()
      xlWorkSheet.Paste()
  
      xlRange.Borders(Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone
      xlRange.Borders(Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone
  
      With xlRange.Borders(Excel.XlBordersIndex.xlEdgeLeft)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With xlRange.Borders(Excel.XlBordersIndex.xlEdgeTop)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With xlRange.Borders(Excel.XlBordersIndex.xlEdgeRight)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlMedium
      End With
      With xlRange.Borders(Excel.XlBordersIndex.xlInsideVertical)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlThin
      End With
      With xlRange.Borders(Excel.XlBordersIndex.xlInsideHorizontal)
          .LineStyle = Excel.XlLineStyle.xlContinuous
          .ColorIndex = 1 'black
          .TintAndShade = 0
          .Weight = Excel.XlBorderWeight.xlThin
      End With
  
  
      xlWorkBook.SaveAs(Filename:="e:\Word1.xls", FileFormat:=56)
      xlWorkBook.Close()
      'xlRange = Nothing
  
      xlApp.Quit()
  
      ReleaseObject(xlRange)
      ReleaseObject(xlWorkSheet)
      ReleaseObject(xlWorkBook)
      ReleaseObject(xlApp)
      End Sub
  
  
      Private Sub ReleaseObject(ByVal obj As Object)
      Try
          Dim intRel As Integer = 0
          Do
          intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
          Loop While intRel &gt; 0
          Debug.Print("Final Released obj # " &amp; intRel)
      Catch ex As Exception
          Debug.Print("Error releasing object" &amp; ex.ToString)
          obj = Nothing
      Finally
          GC.Collect()
      End Try
      End Sub
  End Module
  </code></pre>