Mail Merge through Access VBA

aman

Registered User.
Local time
Today, 10:01
Joined
Oct 16, 2008
Messages
1,251
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
 
I had a similar situation, what I did was use bookmarks on the word document and then used code to assign the form field values to the bookmarks. Found a video on YouTube that explained it.

If you like I can try to find it again and post it...
 
SickLeaveUKReturnToWorkForm.dot

Have not used this code , and wonder what version of word you are using as I am not familiar with .dot , for example access 2003 .doc and 2010 .docx .

Regards Ypma
 
SickLeaveUKReturnToWorkForm.dot

Have not used this code , and wonder what version of word you are using as I am not familiar with .dot , for example access 2003 .doc and 2010 .docx .

Regards Ypma

A .dot file is a word template file before 2007, where they became dotx.
 
Ypma, I am using Microsoft word 2010
 
Gasman thank you for educating me on doc.t I went from 2003 straight to 2010. so missed that .

AMAN If you are using word 2010 shouldn't the word document be name.docx just a thought

Regards Ypma
 
Aman, I see you marked this as "solved". How did you get it sorted out?
 

Users who are viewing this thread

Back
Top Bottom