Listing A Procedure That Creates a Backup Copy of the Active Document on a Floppy Disk

Sub MakeBackup()

Dim currFile As String Dim backupFile As String Const BACKUP_FOLDER = "G:\Backups\" With ActiveDocument

1 Don't bother if the document is unchanged or new

' Mark current position in document

.Bookmarks.Add Name:="LastPosition"

' Turn off screen updating

Application.ScreenUpdating = False

' Save the file


' 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 1 Return to the pre-backup position

Selection.GoTo What:=wdGoToBookmark, Name:="LastPosition"

1 Turn screen updating back on

Application.ScreenUpdating = True End Sub

The procedure opens by declaring some variables, including a constant called BACKUP_ FOLDER that stores the folder to which the backup copy will be saved. (If you plan on using this macro, you'll almost certainly need to modify the value of this constant.) The procedure then checks to see whether 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. (This ensures that when the procedure re-opens the document after running SaveAs later on, you'll be returned to your place in the document.) Bookmarks is a collection that holds all the defined bookmarks in a specified Document object. Each element of this collection is a Bookmark object. To add a bookmark, use the Add method, as follows: Document.Bookmarks.Add Name:= BookmarkName

Document The Document object with which you want to work.

BookmarkName A string that specifies the name of the bookmark.

Then the following statement turns off screen updating, which means you won't see the opening and closing of files that occurs later in the code: Application.ScreenUpdating = False

Then the Save method is used to save the file. You're now ready to perform the backup. First, the currFile variable is used to stored the document's full pathname (that is, the document's drive, folder, and filename), which is given by the FullName property. Then 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 folder specified by BACKUP_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.

Automating the Backup Procedure

Rather than running the MakeBackup procedure by hand, it would be better to schedule backups at specific times or at regular intervals. You can do this by using the Application object's OnTime method, which runs a procedure at a specified time, using the following syntax:

Application.OnTime(When, Name[, Tolerance])

When The time (and date, if necessary) you want the procedure to run. Enter a date/time serial number.

Name The name (entered as text) of the procedure to run when the time given by When arrives.

Tolerance If Word isn't ready to run the procedure at When, it keeps trying for the number of seconds specified by Tolerance. If you omit Tolerance, VBA waits until Word is ready.

The easiest way to enter a time serial number for the When argument is to use the TimeValue function:


Time A string representing the time you want to use (such as "5:00PM" or

For example, the following formula runs the MakeBackup procedure at 5:00 p.m.:

Application.OnTime _

When:=TimeValue("5:00PM"), _ Name:="MakeBackup"

That's fine, but what we really want is the OnTime method to run after a specified time interval (for example, a half hour from now). To make this happen, use Now + TimeValue(Time) for When (where Time is the interval you want to use). For example, the following statement schedules the MakeBackup procedure to run in 5 minutes:

Application.OnTime _

When:=Now + TimeValue("00:05:00"), _ Name:="MakeBackup"

Add this code to the end of the MakeBackup procedure, and Word will automatically run the backup every five minutes.

Working with Text

Although you can add lines, graphics, and other objects to a document, text is what Word is all about. So it won't come as any surprise to you that Word has a truckload of objects that give you numerous ways to work with text. Five of these objects are quite useful: Range, Selection, Words, Sentences, and Paragraphs. The next few sections take you through each of these objects.

0 0

Post a comment