Access.2007.VBA.Bibl..

(John Hannent) #1
Try to locate the template in the default Templates folder, and put up a message if it is not found:

Set fil = fso.GetFile(strTemplateNameAndPath)
If fil Is Nothing Then
strPrompt = “Can’t find “ & strTemplateName _
& “ in “ & strTemplatePath & “; canceling”
MsgBox strPrompt, vbCritical + vbOKOnly
GoTo ErrorHandlerExit
End If

On Error GoTo ErrorHandler

Set doc = appWord.Documents.Add(strTemplateNameAndPath)

Write information to Word doc properties:

Set prps = doc.CustomDocumentProperties
prps.Item(“LetterDate”).Value = Format(Date, “Long Date”)
prps.Item(“RecipientName”).Value = strRecipientName
prps.Item(“RecipientAddress”).Value = strRecipientAddress
prps.Item(“RecipientZip”).Value = strRecipientZip
prps.Item(“Salutation”).Value = strSalutation

Update fields:

With appWord
.Selection.WholeStory
.Selection.Fields.Update
.Selection.HomeKey unit:=wdStory
End With

appWord.Selection.Goto what:=wdGoToBookmark, _
Name:=”LetterText”
strPrompt = “Ready to enter letter text”
strTitle = “Access data imported”
MsgBox strPrompt, vbOKOnly, strTitle
appWord.Visible = True
appWord.Activate

ErrorHandlerExit:
Set appWord = Nothing
Exit Sub

ErrorHandler:
If Err = 429 Then

Word is not running; open Word with CreateObject:

Set appWord = CreateObject(“Word.Application”)
Resume Next

Part I The Office Components and What They Do Best

Free download pdf