![]() |
![]() |
| Consulting | Projects | Home > Technologies > Code > Delete Active Word Document | Site Map | Contact Us |
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:
DeleteActiveDocNormal.NewMacros.DeleteActiveDoc), and drag the associated icon to a toolbarKeyboard... buttonPress 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.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 |