strLongDate = Format(Date, “mmmm d, yyyy”)
strShortDate = Format(Date, “m-d-yyyy”)
strSaveName = “Letter to “ & strContactName
strSaveName = strSaveName & “ on “ & strShortDate _
& “.doc”
strDocsPath = GetContactsDocsPath()
Debug.Print “Docs path: “ & strDocsPath
strTemplatePath = GetContactsTemplatesPath()
Debug.Print “Template path: “ & strTemplatePath
strWordTemplate = strTemplatePath & strWordTemplate
Debug.Print “Word template and path: “ _
& strWordTemplate
Check for the template in the selected Contact Templates folder, and exit if it is not found:
strTestFile = Nz(Dir(strWordTemplate))
Debug.Print “Test file: “ & strTestFile
If strTestFile = “” Then
MsgBox strWordTemplate _
& “ template not found; can’t create letter”
GoTo ErrorHandlerExit
End If
Set the Word Application variable; if Word is not running, the error handler defaults to
CreateObject:
Set appWord = GetObject(Class:=”Word.Application”)
Set docs = appWord.Documents
Set doc = docs.Add(strWordTemplate)
Set prps = doc.CustomDocumentProperties
Turn off error handler because some of the templates may not have all of the doc properties:
On Error Resume Next
prps.Item(“NameTitleCompany”).Value = _
Nz(Me![NameTitleCompany])
prps.Item(“WholeAddress”).Value = _
Nz(Me![WholeAddress])
prps.Item(“Salutation”).Value = _
Nz(Me![Salutation])
prps.Item(“TodayDate”).Value = strLongDate
prps.Item(“CompanyName”).Value = _
strCompanyName
prps.Item(“JobTitle”).Value = _
Nz(Me![JobTitle])
prps.Item(“ZipCode”).Value = _
Nz(Me![ZipCode])
prps.Item(“ContactName”).Value = strContactName
On Error GoTo ErrorHandler
Working with Word Documents and Templates 6