The full DeleteActiveDocument() function without line wrapping

 

Sub DeleteActiveDocument()

Dim strFileToDelete As String
Dim docOpen As Document
Dim intDocCount As Integer

' ************
' Check that there is, in fact, an open document to delete, by checking the documents collection.
' The For...Next loop cycles through the collection and increments a counter by 1 for each open document it finds.
' ************

intDocCount = 0

For Each docOpen In Documents

   intDocCount = intDocCount + 1

Next docOpen

If intDocCount > 0 Then

' ************
' If the counter indicates that there is, indeed, an an open document, check that the user really wants to delete it.
' ************

   If MsgBox("Are you sure you want to delete the open document permanently? " & _

   "You won't be able to undo this action.", vbYesNo) = vbYes Then

   ' ************
   ' If the user wants to delete, check whether the document has already been saved.
   ' ************

      If Len(ActiveDocument.Path) <> 0 Then

      ' ************ 
      ' If it has been saved, close the open document without saving any changes and delete the saved file.
      ' ************ 

         strFileToDelete = ActiveDocument.FullName
         ActiveDocument.Close SaveChanges:=False
         Kill strFileToDelete

      Else

      ' ************
      ' If it hasn't been saved, simply close the open document without saving the changes.
      ' ************

         ActiveDocument.Close SaveChanges:=False

      End If

   ' ************
   ' If the user has changed her/his mind about deleting the document, don't do anything.
   ' ************

   End If

Else

' ************
' If there's no open document, display a message for the user.
' ************

   MsgBox "There is no open document to delete.", vbOKOnly

End If

End Sub

 

 


back to article