I am writing the following lines of code to do word document mail merge through Access/VBA . It opens up the word document but doesn't merge the field values . ANy help will be much appreciated:
Code:
Private Sub cmdSummit_Click()
Call cmdMergeIt
end sub
Private Sub cmdMergeIt()
On Error GoTo ErrorHandler
Dim strSQL As String
strSQL = "SELECT * FROM tblReturnToWork where [RTWID] =" & Me.txtRTWID
Dim strDocumentName As String 'name of the Word template document
strDocumentName = "\SickLeaveUKReturnToWorkForm.dot"
Dim strNewName As String 'name to use when saving
strNewName = "Custom Labels " & Format(CStr(Date), "MMM dd yyyy")
MsgBox "return to cmdmergeit"
Call OpenMergedDoc(strDocumentName, strSQL, strNewName)
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & " occurred. " & Err.Description, vbOKOnly, "Error"
Exit Sub
End Sub
Private Sub OpenMergedDoc(strDocName As String, strSQL As String, strMergedDocName As String)
MsgBox "OpenMergedDoc function"
On Error GoTo WordError
Const strDir As String = "L:\Access Databases\Group Manufacturing\Mortgages Direct\Academy\ADFE\Files\T&C Templates"
Dim objWord As New Word.Application
Dim objDoc As Word.Document
objWord.Application.Visible = True
Set objDoc = objWord.Documents.Open(strDir & strDocName)
objWord.Application.Visible = True
Set objWord = Nothing
Set objDoc = Nothing
Exit Sub
WordError:
MsgBox "Err #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Word Error"
objWord.Quit
End Sub