Access.2007.VBA.Bibl..

(John Hannent) #1
Open a new document based on the selected template:

Set appWord = GetObject(Class:=”Word.Application”)
appWord.Documents.Add strWordTemplate

Write information to Word custom document properties:

Set prps = _
appWord.ActiveDocument.CustomDocumentProperties

Turn off error handler because some templates don’t have all of the doc properties:

On Error Resume Next
prps.Item(“NameTitleCompany”).Value = _
Nz(lst.Column(2, varItem))
prps.Item(“WholeAddress”).Value = _
Nz(lst.Column(5, varItem))
prps.Item(“Salutation”).Value = _
Nz(lst.Column(10, varItem))
prps.Item(“TodayDate”).Value = strLongDate
prps.Item(“CompanyName”).Value = _
strCompanyName
prps.Item(“JobTitle”).Value = _
Nz(lst.Column(8, varItem))
prps.Item(“ZipCode”).Value = _
Nz(lst.Column(6, varItem))
prps.Item(“ContactName”).Value = strContactName

On Error GoTo ErrorHandler

Check for a previously saved document in the documents folder, and append an incremented
number to the save name if one is found:

strDocType = _
appWord.ActiveDocument.
BuiltInDocumentProperties(wdPropertyTitle)
strSaveName = strDocType & “ to “ _
& strContactName & “ - “ & strCompanyName
strSaveName = strSaveName & “ on “ _
& strShortDate & strExtension
i = 2
intSaveNameFail = True
Do While intSaveNameFail
strSaveNamePath = strDocsPath & strSaveName
Debug.Print “Proposed save name and path: “ _
& vbCrLf & strSaveNamePath
strTestFile = Nz(Dir(strSaveNamePath))
Debug.Print “Test file: “ & strTestFile
If strTestFile = strSaveName Then
Debug.Print “Save name already used: “ _
& strSaveName

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf