title: VBA: Error handling on close file
body: <p>I have a VBA code that creates a file and need to have an error handling routine in case something happens in which the file closes (If it is actually open at all). </p>
  
  <p>If the file is open and crashes before it gets to  <em>Close #F</em>, or even before it is actually created.</p>
  
  <pre><code>Sub test1()
  On Error GoTo Err
  
      Dim rs As DAO.Recordset
      Dim F As Integer
  
      F = FreeFile
  
      Open "C:\MYFILE.TXT" For Output As #F
  
      ' My code that does lots of stuff...
  
      If Not rs Is Nothing Then rs.Close
      Close #F
  
  Exit_Sub:
      Set rs = Nothing
      Exit Sub
  Err:
      MsgBox Err.Description
      Resume Exit_Sub 
  
  End Sub
  </code></pre>