152 CHAP TER 8 AU TOM ATE AWAY ANNOYANCES WIT H MACRO S
Double-Save a Document
The Annoyance: Call me paranoid, but I think Word may
really be out to get me—or perhaps its just that I’m working
with master documents. Anyway, Word seems unstable, and I
want to keep a backup copy of each document I’m working
with while I’m working with it. It seems to me that Word’s
automatic backup copy would be just about ideal, if it were a
copy of my most recent save rather than the last-but-one
save. With the speed at which I’m trying to make progress on
my documents, even losing a couple of minutes’ worth of
changes is painful.
The Fix: Create a macro to force Word to save the docu-
ment twice in immediate succession. Call the macro
FileSave
so that it replaces Word’s built-in Save command and picks
up its keyboard shortcut (Ctrl+S).
If you simply want to double-save the document, all you need
is the macro shown in Example 8-9.
The
Options.CreateBackup = True statement ensures that
the “Always create backup copy” feature is on (you can also
set it by choosing ToolsOptions, clicking the Save tab, and
checking the “Always create backup copy” box).
But if you really need to safeguard your work, you can adapt
the macro so that it automatically copies the backup docu-
ment to a safe location—for example, a network drive, as in
Example 8-10.
This macro declares three String variables to store the text it
uses for naming the documents. It assigns to
strDocName the
name of the active document and assigns to
strWordBackupDoc
a string consisting of the full name (including the path) that
Word gives the backup document for the active document.
For example, if the active document is named Example.doc
and is located in the C:\Samples folder, its backup document
is named C:\Samples\Backup of Example.wbk.
The macro assigns to
strMyBackupDoc the path and name for
the backup document that you want to create. In this
example, the macro assigns the backup document the path
Z:\Public\Backups\ (change it as needed, and use the notation
\\server\folder\ if the drive isn’t mapped to a letter), the basic
filename, the word “backup,” the date in yyyy-mm-dd format,
and the time in hh-mm-ss AMPM format. In other words, the
backup document for Example.doc will have a name such as
Example backup 2005-05-27 04-14-38 PM.doc.
Finally, the
FileCopy statement copies the le specified by
strWordBackupDoc to strMyBackupDoc. If there’s already a file
with that name in the backup folder, it will be overwritten,
but unless you save twice within a second, this shouldn’t
happen.
Example 8-9. A macro to save the active document
twice in succession
Sub FileSave()
Options.CreateBackup = True
ActiveDocument.Save
ActiveDocument.Saved = False
ActiveDocument.Save
End Sub
wdan.indb 152
6/13/2005 12:20:05 PM

Get Word Annoyances now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.