NOTE: If you are frustrated by Microsoft Word's lack of a "Delete the document I'm looking at" command, then this macro is for you.
WARNINGS:
- This has only had very limited testing with Word 2000. Your results may vary!
- The files are permanently deleted and not sent to the recycle bin. Do not assign this macro to a key combination or button that you are likely to accidentally hit!
- Macros are often where hackers put in malicious code. If you need to enable macros, do so with caution since you could be opening yourself up for much grief in the future!
Installation Instructions
- Create the Macro
- Open Microsoft Word
- Press Alt F8 or select Tools / Macro / Macros...
- In the first text field, immediately below "Macro name:" enter a name for the macro you are about to create, something like
DeleteActiveDoc
- In the Description area, enter "Permanently Delete the active document"
- Press the "Create" button to create it
- Copy the following code (without line breaks) into the new macro
- Exit the editor and Save the Macro
- Make the new macro easy to find and run (by creating a toolbar button to automatically run the macro)
- Select Tools / Customize to display the "Customize" dialog
- Select the dialog's Commands Tab
- Under "Categories:" (on the dialog's left side), select "Macros"
- Under "Commands:" (on the dialog's right) find your new macro (e.g.,
Normal.NewMacros.DeleteActiveDoc), and drag the associated icon to a toolbar
- Close the Customize Dialog
- Make the new macro easy to find and run (by assigning a key combination to automatically run the macro)
- Select Tools / Customize to display the "Customize" dialog
- Press the dialog's
Keyboard... button
- Again, find Macros under "Categories:" on the right-side, and select your new macro's name on the left so it is highlighted
- Type in a nifty key combo that you would like to use in the future to run the macro -- something like CTRL SHIFT ALT D. Once you type a key combination, it will be displayed in the
Press New Shortcut Key..field. [Note that immediately below this field Word lists if the key combo is already assigned to another command. If so, you may want to try another, unassigned combination. Once you've decided upon a key combination to use, click the Assign and Close buttons in that order
- Try the new macro out (on some extra documents you don't care about!!)
The actual macro code (to be copied) follows:
Option Explicit
Sub DeleteActiveDocument()
'
' DeleteActiveDocument Macro for Microsoft Word 2000
' Macro (c) 7/24/2001, john@active-code.com
'
On Error GoTo ErrorHandler
If Documents.Count < 1 Then
MsgBox "No documents are open"
Else
Dim oFileSystem, theName, aResult, aRecentFile
theName = ActiveDocument.FullName
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
aResult = MsgBox("Are you sure you want to delete " vbCrLf theName "?", _
vbOKCancel vbCritical vbDefaultButton2, "DELETE CURRENT DOCUMENT?!")
If aResult <> vbOK Then
Application.StatusBar = "Deletion of " theName " cancelled!"
Else
ActiveDocument.Close (wdDoNotSaveChanges)
'or ActiveDocument.Close SaveChanges : = wdDoNotSaveChanges
'Delete the file
aResult = oFileSystem.DeleteFile(theName, True)
'Delete file listing from Recent File list
For Each aRecentFile In RecentFiles
If aRecentFile.Path "\" aRecentFile.Name = theName Then
aRecentFile.Delete
Exit For
End If
Next
Application.StatusBar = "DELETED: " theName
End If
End If
Exit Sub
ErrorHandler:
If Err.Number <> 53 Then
MsgBox "Unexpected Error #" CStr(Err.Number) " (" Err.Description ") while deleting " theName
Else
MsgBox "Unable to find file '" theName "' for deletion. Possibly not saved yet."
End If
Resume Next
End Sub