Function Create_Word_Documents()
On Error GoTo Errorhandler
Dim dbs As Database
Dim rst As Recordset
Dim strCriteria As String
Dim strSearchPath As String
Dim strSavePath As String
Dim strDocumentName As String
Dim strDescription As String
Dim strNewDescription As String
Dim strFileName As String
Dim intX As Integer
Dim msg, style, resp
Dim objWord As Object
Dim objAccess As Object
'Letter Templates are already created for the user to select
'The templates have bookmarks created to receive data from the database
strSearchPath = DLookup("LettersPath", "Control_Parameters")
If IsNull(strSearchPath) Then
strSearchPath = "C:\Access Databases\My Letter Path\Word Documents"
End If
strSavePath = DLookup("CustomersLettersPath", "Control_Parameters")
If IsNull(strSavePath) Then
strSavePath = "C:\Access Databases\My Letter Path\Customer Letters"
End If
strDocumentName = OpenWordDocument(strSearchPath)
If IsNull(strDocumentName) Or strDocumentName = "" Then
msg = "No document has been selected!"
style = vbOKOnly + vbInformation
MsgBox msg, style
Exit Function
End If
'A simple routine to extract the document name without the path
For intX = Len(strDocumentName) To 1 Step -1
If Mid(strDocumentName, intX, 1) = "\" Then
strDescription = Mid(strDocumentName, intX + 1, 99)
GoTo End_Name:
End If
Next intX
End_Name:
'Can be driven from a number of forms and data
If Not IsLoaded("frmCustomerLettersExistingAbstracts") Then
DoCmd.OpenForm "frmCustomerLettersSelect", , , , , acDialog
End If
DoCmd.OpenForm "frmCustomerLetters", , , , acFormAdd, acDialog
DoCmd.Close acForm, "frmCustomerLettersSelect"
If Not IsLoaded("frmCustomerLetters") Then
Exit Function
End If
' In this case you could also include paragraphs stored in the database and insert them in the document
'msg = "Do you want to include any Standard Paragraphs ?"
'style = vbYesNo + vbQuestion + vbDefaultButton2
'resp = MsgBox(msg, style)
'If resp = vbYes Then
' DoCmd.OpenForm "frmSelectParagraphs", , , , , acDialog
'End If
'Because this is linked to the database the user could create their own description
strNewDescription = Mid(strDescription, Len(strDescription) - 3, 4)
If strNewDescription = ".doc" Then
strDescription = Mid(strDescription, 1, Len(strDescription) - 4)
End If
strNewDescription = InputBox("Enter a new Description for this document", , strDescription)
If strNewDescription = "" Then
strNewDescription = strDescription
End If
'The new description is written back to the form and subsequently the database
'A user could then get all letters written to a customer and view them in word as saved
If IsLoaded("frmCustomerLetters") Then
[Forms]![frmCustomerLetters]![Description] = strNewDescription
End If
strFileName = strSavePath & "\" & [Forms]![frmCustomerLetters]![LetterUniqueNo] & ".doc"
[Forms]![frmCustomerLetters]![WordDocument] = strFileName
'Takes a copy of the template so it is not overwritten - all changes are to the copy
FileCopy strDocumentName, strFileName
'Calls the subroutine below to open word
WordDocuments (strFileName)
Do
Err = 0
Set objWord = GetObject(, "Word.Application")
Loop While Err <> 0
'loads the bookmarks with the data
With objWord.ActiveDocument
If .Bookmarks.Exists("LetterDate") = True Then
.Bookmarks("LetterDate").Select
If Not IsNull([Forms]![frmCustomerLetters]!LetterDate) Then
objWord.Selection.Text = (CStr(Format([Forms]![frmCustomerLetters]!LetterDate, "Long Date")))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("CustomerName") = True Then
.Bookmarks("CustomerName").Select
If Not IsNull([Forms]![frmCustomerLetters]!CustomerName) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!CustomerName))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("AddressLine1") = True Then
.Bookmarks("AddressLine1").Select
If Not IsNull([Forms]![frmCustomerLetters]!PostalAddress1) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!PostalAddress1))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("AddressLine2") = True Then
.Bookmarks("AddressLine2").Select
If Not IsNull([Forms]![frmCustomerLetters]!PostalAddress2) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!PostalAddress2))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("AddressLine3") = True Then
.Bookmarks("AddressLine3").Select
If Not IsNull([Forms]![frmCustomerLetters]!PostalAddress3) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!PostalAddress3))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("AddressLine4") = True Then
.Bookmarks("AddressLine4").Select
If Not IsNull([Forms]![frmCustomerLetters]!PostalAddress4) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!PostalAddress4))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("AuthorName") = True Then
.Bookmarks("AuthorName").Select
If Not IsNull([Forms]![frmCustomerLetters]!AbstractAuthor) Then
objWord.Selection.Text = (CStr("Attention: " & [Forms]![frmCustomerLetters]!AbstractAuthor))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("MatterNo") = True Then
.Bookmarks("MatterNo").Select
If Not IsNull([Forms]![frmCustomerLetters]!AbstractClient) Then
'objWord.Selection.Text = (CStr("RE: " & [Forms]![frmCustomerLetters]!AbstractClient))
objWord.Selection.Text = (CStr("RE: " & UCase([Forms]![frmCustomerLetters]!AbstractClient)))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("OurRef") = True Then
.Bookmarks("OurRef").Select
If Not IsNull([Forms]![frmCustomerLetters]!AbstractNo) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!AbstractNo))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("StaffName1") = True Then
.Bookmarks("StaffName1").Select
If Not IsNull([Forms]![frmCustomerLetters]!StaffName) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!StaffName))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("StaffName2") = True Then
.Bookmarks("StaffName2").Select
'If Not IsNull([Forms]![frmCustomerLetters]!StaffName) Then
' objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!StaffName))
'Else
' objWord.Selection.Text = ""
'End If
If Not IsNull([Forms]![frmCustomerLetters]!StaffDetails) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!StaffDetails))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("ExpiryDate") = True Then
.Bookmarks("ExpiryDate").Select
If Not IsNull([Forms]![frmCustomerLetters]!ExpiryDate) Then
objWord.Selection.Text = (CStr([Forms]![frmCustomerLetters]!ExpiryDate))
Else
objWord.Selection.Text = ""
End If
End If
If .Bookmarks.Exists("ParagraphText") = True Then
If IsLoaded("frmSelectParagraphs") Then
Set dbs = CurrentDb
strCriteria = "SELECT Word_Document_Paragraphs.ParagraphText " _
& "FROM tmpParagraphSelect LEFT JOIN Word_Document_Paragraphs ON tmpParagraphSelect.ParagraphNo " _
& "= Word_Document_Paragraphs.UniqueNo " _
& "WHERE tmpParagraphSelect.SelectParagraph = True " _
& "ORDER BY tmpParagraphSelect.SortSequence;"
Set rst = dbs.OpenRecordset(strCriteria, dbOpenSnapshot)
objWord.ActiveDocument.Bookmarks("ParagraphText").Select
objWord.Selection.Text = ""
Do Until rst.EOF
objWord.Selection.InsertAfter rst!ParagraphText
objWord.Selection.InsertAfter vbCrLf
objWord.Selection.InsertAfter vbCrLf
rst.MoveNext
Loop
DoCmd.Close acForm, "frmSelectParagraphs"
rst.Close
Set dbs = Nothing
Else
objWord.ActiveDocument.Bookmarks("ParagraphText").Select
objWord.Selection.Text = ""
End If
End If
End With
'The document is left open for further modification if necessary and must be saved and closed as a usual document is.
'Note that word must be closed to have Access as the active object on completion.
objWord.ActiveWindow.WindowState = wdWindowStateMaximize
DoCmd.Close acForm, "frmCustomerLetters", acSaveYes
Exit Function
Errorhandler:
Call Error_Display(Err)
End Function