Solved

Automate Mail Merge Within Access 2010

Posted on 2014-11-14
7
776 Views
Last Modified: 2014-12-04
Been using Access for a very long time but this is the first time I want to do a mail merge.

I would like everything to work from within Access using VBA Code.

For testing purposes I create a Mail Merge document called 'MailMergeTest.docx' in directory 'C:\My Documents\Word Documents'.

It is linked to 'tblProperty' in my Access database.  I can run it manually from Word 2010 but want the output generated automatically from within Access, when the user requests.

How do I accomplish that?
0
Comment
Question by:mlcktmguy
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Expert Comment

by:bonjour-aut
ID: 40444211
it takes some efforts;
lets assume you have prepared a word document with some bookmarks you want to replace by code.
you need to operate the complete word operations - 1. open the template.doc,2.  replace the bookmarks with variable data, 3. save the document with valid name

example code:

Private Sub cmdDoc_Click()
 
  Dim myTemplate, SName As String
  Dim wdAnw As Word.Application
  Dim wdDok As Word.Document
  Dim myBk As Word.Bookmark

 
  Set wdAnw = CreateObject("Word.Application")
  myTemplate = "someTemplate.dot" 'enter your template name
  Set wdDok = wdAnw.Documents.Add(Template:=myTemplate)
 
  If Nz(wdDok.Bookmarks.Count, 0) = 0 Then Exit Sub
 
  For Each myBk In wdDok.Bookmarks
    wdDok.Bookmarks(myBk).Range.Text = DLookup("myContent", "Dokument", "myMark='" & myBk.Name & "'")
  Next

  SName = "someFilepath&Name"
  pubDocFile = SName
  wdAnw.ActiveDocument.SaveAs (SName)
  wdAnw.Quit
  Set wdAnw = Nothing
  Application.FollowHyperlink (SName)
 
End Sub

!!! dont forget to add Ms-Word to the VBA references
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40452350
Thank you, I got unexpectently called away and probably won't get to try this until at least next week.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40463610
@bonjour-aut
Is that a mail merge?
My code for mail merge looks very different

       
'open the mergedoc
        Dim oApp As Object
        Dim MlMrge As Object
        Set oApp = CreateObject(Class:="Word.Application")
        oApp.Visible = True
        
        'this will open a new document based on the path as a template.  Excellent
        'but it doesn't open it as a mail merge
        oApp.Documents.Add "C:\My Documents\Word Documents\MailMergeTest.docx "
        ' so start a merge
        Set MlMrge = oApp.ActiveDocument.MailMerge
        With MlMrge
            'do the merge, get the datasource -- and be tricky if the Access app is opened exclusively!
            .OpenDataSource Name:="c:\temp\myapp.mdb", _
 LinkToSource:=True, AddtoRecentFiles:=False, _
 Connection:="TABLE [tblProperty]" ', _
 SQLStatement:="SELECT * FROM [tblProperty]"
             'show the field values
            .ViewMailMergeFieldCodes = 9999998 'wdToggle
            'merge to a new document
             'do you want merge to email instead
            .Destination = 0 'wdSendToNewDocument
            .SuppressBlankLines = True
            'blow through all the records
            With .DataSource
                .FirstRecord = 1 'wdDefaultFirstRecord
                .LastRecord = -16 'wdDefaultLastRecord
            End With
            .Execute (False) 'execute and don't stop for errors -- list them in a new Word document, if any.
        End With

Open in new window

This is merging one new document per record in the table -- so be prepared for a blizzard of Word documents!
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 18

Expert Comment

by:bonjour-aut
ID: 40463996
@Nick67

Your argument, that the example code is not a classical mail merge is correct. It just deals with the Q of variable data in word documents.
I felt, that to solve the task in clearly seperated steps will be beneficial for mlcktmguy.
For final solution of sending the mails, there are at least 3 different methods possible, depending on what mail-server infrastructure is desired/available:
1. You want to use MS-Outlook available on the same machine, as the application is running - You will have all Mails sent in the sent-folder. If you do not plan for a follow up, this may be disturbing.
2. You want to use the CDO - component, which directly works with the SMTP server (http://msdn.microsoft.com/en-us/library/ms526318%28v=exchg.10%29.aspx)
3. You want to use some web-service, either commercial or a own PHP script.

My clients love CDO version for informing their customers on new offerings.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40468356
Thanks for the responses, I should get to work on this again very soon.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 40473746
Nick67:  I will be executing the mail merge from an .accdb that includes 'tblProperty' as a linked table.

What does the '.OpenDataSource'  look like in that scenario?
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40474171
I think I wrote it in correctly, but not as an accdb
.OpenDataSource Name:="c:\temp\myapp.mdb", _
 LinkToSource:=True, AddtoRecentFiles:=False, _
 Connection:="TABLE [tblProperty]" ', _
 SQLStatement:="SELECT * FROM [tblProperty]"

Alter as required in bold
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question