colinmitton
Technical User
Below is my code to run a mail merge in word 2010 using a template with the links to the DB and relevent field inserted.
It works great and I'm happy with it, well nearly!
For every different mail merge I do from different table sources i.e. 3rd party business or contacts or clients, etc...
Will require its own merge template with the relevent fields for the different tables in place. this could end up with loads of different template letters which will get out of hand. There is a way of creating 'generic' fields in word but how do I tell access that a field from a table to the generic name (i.e. 'cl_Address1' from the client table to 'add1' field in the word document).
I hope this makes sense. my code...
It works great and I'm happy with it, well nearly!
For every different mail merge I do from different table sources i.e. 3rd party business or contacts or clients, etc...
Will require its own merge template with the relevent fields for the different tables in place. this could end up with loads of different template letters which will get out of hand. There is a way of creating 'generic' fields in word but how do I tell access that a field from a table to the generic name (i.e. 'cl_Address1' from the client table to 'add1' field in the word document).
I hope this makes sense. my code...
Code:
Private Sub Cmd_merge_to_word_Click()
Dim strBusID As String
Dim strContID As String
Dim wdApp As New Word.Application
Dim wdMMDoc As Word.Document
' Get the Info for the where statement on the SQLStatement
strBusID = "[3rdPartyBusinessID] = " & [3rdPartyBusinessID] & ""
strContID = "[3rdPartyContactID] = " & [3rdPartyContactID] & ""
' Open the merge document and give it a name!
Set wdMMDoc = wdApp.Documents.Add("c:\Access\Merge.dotx")
' wdApp.Visible = False
wdApp.Visible = True
' Connect to datasource and run sqlstatement to pick the info from the form
wdApp.ActiveDocument.MailMerge.OpenDataSource _
Name:=Application.CurrentProject.FullName, _
OpenExclusive:=False, _
LinkToSource:=True, _
Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=c:\Access\DBv2.accdb;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet" _
, SQLStatement:="SELECT * FROM `qry_3rd_P_address_merge` Where " & strBusID & " And " & strContID
' Run the merge
wdMMDoc.MailMerge.Execute
' Close the Merge Document
wdMMDoc.Close False
wdApp.Visible = True
' Bring the merged letter to the front
wdApp.Activate
' tidy up
Set wdApp = Nothing
Set wdMMDoc = Nothing
End Sub