John Cornelison active-code.com
   Consulting   |    Projects   |    Home > Technologies > Code > Delete Active Word Document   |    Site Map   |    Contact Us   

Delete Active Word Document

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:

Installation Instructions

  1. Create the Macro
    1. Open Microsoft Word
    2. Press Alt+F8 or select Tools / Macro / Macros...
    3. In the first text field, immediately below "Macro name:" enter a name for the macro you are about to create, something like DeleteActiveDoc
    4. In the Description area, enter "Permanently Delete the active document"
    5. Press the "Create" button to create it
    6. Copy the following code (without line breaks) into the new macro
    7. Exit the editor and Save the Macro
  2. Make the new macro easy to find and run (by creating a toolbar button to automatically run the macro)
    1. Select Tools / Customize to display the "Customize" dialog
    2. Select the dialog's Commands Tab
    3. Under "Categories:" (on the dialog's left side), select "Macros"
    4. Under "Commands:" (on the dialog's right) find your new macro (e.g., Normal.NewMacros.DeleteActiveDoc), and drag the associated icon to a toolbar
    5. Close the Customize Dialog
  3. Make the new macro easy to find and run (by assigning a key combination to automatically run the macro)
    1. Select Tools / Customize to display the "Customize" dialog
    2. Press the dialog's Keyboard... button
    3. Again, find Macros under "Categories:" on the right-side, and select your new macro's name on the left so it is highlighted
    4. 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.
    5. Once you've decided upon a key combination to use, click the Assign and Close buttons in that order
  4. 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

   Consulting   |    Projects   |    Home > Technologies > Code > Delete Active Word Document   |    Site Map   |    Contact Us   
TOP