Tip: Delete the open document in Microsoft Word
Wednesday, July 23, 2008 at 05:00PM Have you ever wanted to delete the document currently open in Word? I find I often want to do this when I’m trying to clean out a whole bunch of old documents: I open each one, check the contents and, if I no longer need it, delete it on the spot.
Word won’t normally let you do this: you must first close the document, then delete it. That’s a sensible safeguard, but sometimes it gets in the way of what you want to do.
Using a macro, you can get around this restriction and delete the active document. Note that because you cannot undelete any document you eliminate using this macro, it takes some preliminary precautions:
- First, the macro checks that there is, in fact, an open document to delete.
- If there is an open document, it checks that you really want to delete it. If not, the macro ends without doing anything else.
- If you give the go ahead to delete the document, the macro checks to see whether the open document has already been saved. If it has, the document is closed and then deleted. If it has not yet been saved, it is simply closed without saving any changes.
To create the macro:
- Press Alt+F8 to open the Macros dialog.
- Type DeleteActiveDocument in the Name box and click Create.
- Copy and paste the following code between the Sub DeleteActiveDocument()and the End Sub statements (Note: if you want, you can download a full text copy of the code, complete with comments describing what it’s doing at each stage):
Sub DeleteActiveDocument()
Dim strFileToDelete As String
Dim docOpen As Document
Dim intDocCount As Integer
intDocCount = 0
For Each docOpen In Documents
intDocCount = intDocCount + 1
Next docOpen
If intDocCount > 0 Then
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 Len(ActiveDocument.Path) <> 0 Then
strFileToDelete = ActiveDocument.FullName
ActiveDocument.Close SaveChanges:=False
Kill strFileToDelete
Else
ActiveDocument.Close SaveChanges:=False
End If
End If
Else
MsgBox “There is no open document to delete.”, vbOKOnly
End If
End Sub
- Close the Visual Basic editor.
Add it to Word’s toolbar
To make the macro easily accessible, add it to the Quick Access Toolbar in Word 2007:
- Click the arrow at the right end of the Quick Access Toolbar and select More Commands.
- In the Choose Commands From box, select Macros from the drop-down list.
- Click the newly created macro (it’s full name is Normal.NewMacros.DeleteActiveDocument) and then click the Add button.
- Click the Modify button, select an appropriate icon for the macro (there’s a handy one with a cross in a red circle that fits well), type Delete This Doc in the Display Name box and click OK.
To add the macro to a Word 2003 toolbar:
- Right-click any toolbar and click Customize.
- On the Commands tab in the Categories list click Macros.
- Locate Normal.NewMacros.DeleteActiveDocument in the Commands list and click-and-drag it onto a toolbar.
- Right-click the new button you’ve created, in the Name box type Del (or something else short and descriptive), then click Text Only. If you’d prefer to use a graphic icon instead of a text-label, although none of the default icons is particularly apt you can create your own by selecting Default (instead of Text Only). Then click Edit Button Image, then Clear and then do a quick paint job (a red X works well).
- Click Close in the Customize dialog box.
If you have any problems running the macro, take a look at the code in the Visual Basic editor and make sure you don’t have any broken lines. That’s the usual cause of problems.
Rose Vines |
4 Comments |
delete,
macros,
microsoft word,
tips in
Microsoft Office,
Microsoft Word,
software,
tips,
word processing 




Hobbling Australia's Internet
The recently elected Australian Government has done some wonderful things and some completely boneheaded things. Its current plan to filter the Internet definitely belongs in the latter category.
If you haven’t heard about this, here’s a brief rundown. The Australian Commonwealth Government is introducing legislation that will force all Australian servers to filter Internet traffic and block any sites deemed inappropriate. The government gets to decide which sites earn that “inappropriate” tag and the resulting blacklist will not be made public in its entirety.
Let’s put to one side the political reasons why this is a bad idea (that’s quite a pile we’ll have on the side) and simply focus on the impracticalities. Testing has already shown that:
Almost everyone who has a practical understanding of the Internet has said this is a stupid, unworkable plan. But you know governments: sometimes that urge to be paternalistic trumps commonsense. This is one of those times.
GetUp is organising a campaign to stop the idiocy and save the Internet for all Australians. If you’re from my homeland, I urge you to add your signature to GetUp’s petition and support its campaign. You can do so using the box below. [Update: The campaign has closed, so I’ve removed the box as it was slowing the loading of this page.] You can also learn more about the scheme by reading GetUp’s Factsheet.