title: Can't Close and Delete Open MSWord 2010 Document (ActiveDocument.Close prevents Kill command from running.)
body: <p>I'm trying to close the open word document and delete it as part of my DocumentBeforeClose event. I keep running across the following code in my searches (+/- some error trapping statements): </p>
  
  <pre><code>Dim Killfile As String
  Killfile = Dir$(ActiveDocument.FullName)
     If Len(Dir$(Killfile)) &gt; 0 Then
        SetAttr Killfile, vbNormal
        ActiveDocument.Close False
        Kill Killfile
     End if
  </code></pre>
  
  <p>I've found similar code (well, frankly almost identical code) in several places, and everyone seems to confirm that it works, but I cannot get it to run correctly in my own attempts, regardless of what module I have the code in. When run, the document always closes, but the code also stops there and never gets to the Kill command. In theory the instance of Word is supposed to keep running the script once the document closes; in my case the instance is open, but there's nothing running anymore at that point. Am I doing something obviously wrong here? </p>
  
  <p>If it's helpful, in another version of the code I'm working with I have a Do While loop going through a directory and deleting documents containing "<em>temp.doc</em>" in the name. That code has no trouble running through and deleting closed files, but also will not delete the active/open document after closing.</p>
  
  <p>Is this something I need to place in the Normal module in order for it to work? (Would reeeaally like to avoid that if possible). Thanks for any help!</p>
  
  <p>Editing to add the top few relevant Search Results from Googling "MSWord VBA Delete Open Document":</p>
  
  <ul>
  <li><a href="http://word.tips.net/T001346_Deleting_the_Open_Document_File.html" rel="nofollow noreferrer">http://word.tips.net/T001346_Deleting_the_Open_Document_File.html</a></li>
  <li><a href="http://geekgirls.com/2010/02/deleting-an-open-document-in-microsoft-word/" rel="nofollow noreferrer">http://geekgirls.com/2010/02/deleting-an-open-document-in-microsoft-word/</a></li>
  <li><a href="https://stackoverflow.com/questions/24679118/delete-doc-when-macro-finishes">Delete doc when macro finishes</a></li>
  </ul>
  
  <p>Again, the unifying factor appears to be: </p>
  
  <pre><code>Activedocument.close
  Kill WhateverOpenFile
  </code></pre>
  
  <p>...which just plain doesn't seem to make sense. </p>