I didn't use the select case statement. Instead, I formed a table with a Value field and make the buttonclick choose the path based on the Value. However, for some reasons, the bookmarks wouldn't work. For example, I have 7 bookmarks for all 5 templates, but each template don't use all of them. When I ran mail merged program, ACCESS kept replacing the last bookmark used in the template with the last bookmark stated in the code, even if the bookmark is not used in this template. Any idea on how to fix this?
Here is my code
Code:
Option Compare Database
Public Function CreateWordLetter(strDocPath As String)
'if no path is passed to function, exit
If IsNull(strDocPath) Or strDocPath = "" Then
Exit Function
End If
Dim dbs As Database
Dim objWord As Object
Dim PrintResponse
Set dbs = CurrentDb
'create reference to Word Object
Set objWord = CreateObject("Word.Application")
'Word Object is created - now let's fill it with data
With objWord
.Visible = True
.Documents.Open (strDocPath)
'move to each bookmark, and insert correct text.
.ActiveDocument.Bookmarks("firstname").Select
.Selection.Text = (CStr(Forms!MailMerge!strMomFirstName))
On Error Resume Next
.ActiveDocument.Bookmarks("address").Select
.Selection.Text = (CStr(Forms!MailMerge!UpdatedMomAddress))
On Error Resume Next
.ActiveDocument.Bookmarks("city").Select
.Selection.Text = (CStr(Forms!MailMerge!UpdatedMomCity))
On Error Resume Next
.ActiveDocument.Bookmarks("state").Select
.Selection.Text = (CStr(Forms!MailMerge!UpdatedMomState))
On Error Resume Next
.ActiveDocument.Bookmarks("zip").Select
.Selection.Text = (CStr(Forms!MailMerge!UpdatedMomZip))
On Error Resume Next
.ActiveDocument.Bookmarks("firstname2").Select
.Selection.Text = (CStr(Forms!MailMerge!strMomFirstName))
On Error Resume Next
.ActiveDocument.Bookmarks("lastname").Select
.Selection.Text = (CStr(Forms!MailMerge!strMomLastName))
On Error Resume Next
End With
'find out if the user would like to print the document at this time.
PrintResponse = MsgBox("Print this document?", vbYesNo)
If PrintResponse = vbYes Then
objWord.ActiveDocument.PrintOut Background:=False
End If
'release all objects
Set objWord = Nothing
Set dbs = Nothing
End Function
Private Sub cbxTemplate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub
Private Sub cmdMailMerge_Click()
If Not Len(cbxTemplate.Value) = 0 Then
CreateWordLetter (cbxTemplate.Value)
Else
MsgBox "Select Mail Merge process"
End If
If cbxTemplate.Value = "Z:\Studies\Cleft Utah New CDC Study\Utah mother file\ThankYou.docx" Then
DateofThankYouLetterSent = Date
TrackingStatus = 8
Else
DateofLetterSent = Date
TrackingStatus = 2
End If
End Sub
Private Sub Combo121_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub