Else
MsgBox “Error No: “ & Err.Number _
& “; Description: “ & Err.Description
Resume ErrorHandlerExit
End If
End Sub
Figure 6.16 shows the resulting letter.
When creating Word documents in VBA code, I save the current date to the TodayDate
document property in the export code, rather than inserting a date code into the Word
template, to ensure that the date on the letter will always be the date the letter was created; a Date
field will show the current date (the date the letter is reopened).
If the name has already been used, the code loops back and adds a number to the end of the save
name, and keeps trying until an unused number is reached. This technique means that you won’t
overwrite documents created the same day, but instead will create a series of documents with incre-
menting numbers.
FIGURE 6.16
A Word letter filled with Access data from a single contact record.
TIPTIP
Working with Word Documents and Templates 6