Home > Articles > Home & Office Computing > Microsoft Applications

This chapter is from the book

Making Backups as You Work

Even if you're using the macros earlier in the chapter to save your work frequently, you can still lose data if your hard drive crashes. So we've all learned from hard experience not only to save our work regularly, but also to make periodic backup copies. The macro I use most often in Word is one that does both in a single procedure! That is, the macro not only saves your work, but it also makes a backup copy on another drive, such as a removable disk, a second hard drive, or a network folder. Listing 3.5 shows the code.

Listing 3.5. A Procedure That Creates a Backup Copy of the Active Document on Another Drive

Sub MakeBackup()
    Dim currFile As String
    Dim backupFile As String
    Const BACKUP_FOLDER = "G:\Backups\"
    With ActiveDocument
        '
        ' Don't bother if the document is unchanged or new
        '
        If .Saved Or .Path = "" Then Exit Sub
        '
        ' Mark current position in document
        '
        .Bookmarks.Add Name:="LastPosition"
        '
        ' Turn off screen updating
        '
        Application.ScreenUpdating = False
        '
        ' Save the file
        '
        .Save
        '
        ' Store the current file path, construct the path for the
        ' backup file, and then save it to the backup drive
        '
        currFile = .FullName
        backupFile = BACKUP_FOLDER + .Name
        .SaveAs FileName:=backupFile
    End With
    '
    ' Close the backup copy (which is now active)
    '
    ActiveDocument.Close
    '
    ' Reopen the current file
    '
    Documents.Open FileName:=currFile
    '
    ' Return to the pre-backup position
    '
    Selection.GoTo What:=wdGoToBookmark, Name:="LastPosition"
End Sub

The backupFile and currFile variables are strings that store the full pathnames for the active document and the backup version of the document. Use the BACKUP_FOLDER constant to specify the folder in which you want the backup stored.

The procedures first check to see if the backup operation is necessary. In other words, if the document has no unsaved changes (the Saved property returns True) or if it's a new, unsaved document (the Path property returns ""), bail out of the procedure (by running Exit Sub).

Otherwise, a new Bookmark object is created to save the current position in the document, screen updating is turned off, and the file is saved.

We're now ready to perform the backup. First, the currFile variable is used to store the full pathname of the document, and the pathname of the backup file is built with the following statement:

backupFile = BACKUP_FOLDER + .Name

This is used to save the file to the specified folder. The actual backup takes place via the SaveAs method, which saves the document to the path given by backupFile. From there, the procedure closes the backup file, reopens the original file, and uses the GoTo method to return to the original position within the document.

  • + Share This
  • 🔖 Save To Your Account