On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Query1")
rs.MoveFirst
Dim docs As Word.Documents
Dim strWordTemplate As String
Dim strDocsPath As String
Dim strTemplatePath As String
Dim prps As Object
Dim strShortDate As String
Dim strLongDate As String
Dim strTest As String
Dim strAddress As String
Dim strCountry As String
Dim strSaveName As String
Dim strTestFile As String
Dim intSaveNameFail As Boolean
Dim i As Integer
Dim strSaveNamePath As String
'Set global Word application variable; if Word is not running,
'the error handler defaults to CreateObject
Set pappWord = GetObject(, "Word.Application")
strLongDate = Format(Date, "mmmm d, yyyy")
strShortDate = Format(Date, "m-d-yyyy")
strSaveName = "SampleFBDS403 " & rs("CustomerName").Value & " "
strSaveName = strSaveName & " on " & strShortDate & ".doc"
strDocsPath = "c:\temp\"
strTemplatePath = "c:\temp" 'pappWord.Options.DefaultFilePath(wdUserTemplatesPath)
strWordTemplate = strTemplatePath & "\" & "FBDS403Template.dot"
'Check for existence of template in template folder,
'and exit if 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
'Check for existence of previously saved letter in documents folder,
'and append an incremented number to save name if found
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
'Create new save name with incremented number
intSaveNameFail = True
strSaveName = "Letter " & CStr(i) & " to " & _
Me![FirstName] & " " & Me![LastName]
strSaveName = strSaveName & " on " & strShortDate & ".doc"
strSaveNamePath = strDocsPath & strSaveName
Debug.Print "New save name and path: " _
& vbCrLf & strSaveNamePath
i = i + 1
Else
Debug.Print "Save name not used: " & strSaveName
intSaveNameFail = False
End If
Loop
Set docs = pappWord.Documents
docs.Add strWordTemplate
Set prps = pappWord.ActiveDocument.CustomDocumentProperties
prps.Item("Customer").Value = rs("CustomerName").Value
prps.Item("Site").Value = rs("Sites").Value
prps.Item("Address").Value = rs("ShipToSiteAddress1").Value
prps.Item("Commission").Value = rs("Commission").Value
prps.Item("ShortDescription").Value = rs("Change").Value
prps.Item("Reason").Value = rs("Reason").Value
prps.Item("AdditionalInformation").Value = rs("AdditionalInformation").Value
With pappWord
.Visible = False
.Selection.WholeStory
.Selection.Fields.Update
MsgBox "Going to save as " & strSaveNamePath
.ActiveDocument.SaveAs strSaveNamePath
'.Activate
If (MsgBox("Open the word document file?", vbYesNo, "Open Document " & strSaveNamePath & "?") = vbYes) Then
.Visible = True
.Activate
Else
.Visible = False
End If
.Selection.EndKey Unit:=wdStory
End With
ErrorHandlerExit:
Set pappWord = Nothing
Exit Sub
ErrorHandler:
'Word is not running; open Word with CreateObject
If Err.Number = 429 Then
Set pappWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If