title: VBA script to close every instance of Excel except itself
body: <p>I have a subroutine in my errorhandling function that attempts to close every workbook open in every instance of Excel. Otherwise, it might stay in memory and break my next vbscript.  It should also close every workbook <em>without</em> saving any changes.</p>
  <pre><code>Sub CloseAllExcel()
  On Error Resume Next
      Dim ObjXL As Excel.Application
      Set ObjXL = GetObject(, &quot;Excel.Application&quot;)
      If Not (ObjXL Is Nothing) Then
          Debug.Print &quot;Closing XL&quot;
          ObjXL.Application.DisplayAlerts = False
          ObjXL.Workbooks.Close
          ObjXL.Quit
          Set ObjXL = Nothing
      Else
          Debug.Print &quot;XL not open&quot;
      End If
  End Sub
  </code></pre>
  <p>This code isn't optimal, however.  For example, it can close 2 workbooks in one instance of Excel, but if you open 2 instances of excel, it will only close out 1.</p>
  <p>How can I rewrite this to close <em>all</em> Excel without saving any changes?</p>
  <h3>Extra Credit:</h3>
  <p>How to do this for Access as well without closing the Access file that is hosting this script?</p>