Mail Merge in VBA

Posted on 2005-04-26
Last Modified: 2010-05-02

I want SINGLE DOCUMENT as output.This single doc must have one letter every page. My following code produces a letter in separate document. Pls give the code where i can create one single doc with all letters/page.

This is VBA scipt in word.


Set New Document and save it with EventNbr postfix
Set NewDoc = ActiveDocument
Savepath$ = exepath$
File_Name$ = Savepath$ + "\Train2_OutputFile" + CurrRec$
NewDoc.SaveAs FileName:=File_Name$

If CurrentRecord < EndRecord Then
  MainDoc.Close SaveChanges:=wdDoNotSaveChanges
  WordBasic.FileNew template:=template$
End If
Question by:mahalakshmi_s
    1 Comment
    LVL 1

    Accepted Solution

    '// ************************************************************************ //
    '// Creates a MailMerge via Word-Basic
    '// DataSource: via MS-Access Database
    '// - Use the WinWord-MailMerge-Toolbar to
    '//   add the Database-Fields in your Word-Template.
    '// Example: MailMerge "", "MyData.mdb", "Adresses", "Output.mdb"
    '// ************************************************************************ //
    Private Sub MyMailMerge(strTemplateFileName As String, _
                          strDataFileName As String, _
                          strTableName As String, _
                          strNewDocumentFileName As String)
      'New Document
      Dim oDocument As Word.Document
      Set oDocument = Application.Documents.Add(strTemplateFileName)  '
      Dim oMerger As Word.MailMerge
      Set oMerger = oDocument.MailMerge
      oMerger.OpenDataSource _
                Name:=strDataFileName, _
                LinkToSource:=True, _
                AddToRecentFiles:=False, _
                Connection:="TABLE " & strTableName, _
      oMerger.SuppressBlankLines = False
      oMerger.Destination = wdSendToNewDocument

      oDocument.SaveAs strNewDocumentFileName
    End Sub

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now