• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Mail Merge in VBA

Hi,

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.

Thanks
M

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$
NewDoc.Close

If CurrentRecord < EndRecord Then
  MainDoc.Close SaveChanges:=wdDoNotSaveChanges
  WordBasic.FileNew template:=template$
End If
0
mahalakshmi_s
Asked:
mahalakshmi_s
1 Solution
 
wuerziCommented:
'// ************************************************************************ //
'// 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 "MyTemplate.dot", "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)  'MyTemplate.dot
 
  'DataBinding
  Dim oMerger As Word.MailMerge
  Set oMerger = oDocument.MailMerge
  oMerger.OpenDataSource _
            Name:=strDataFileName, _
            LinkToSource:=True, _
            AddToRecentFiles:=False, _
            Connection:="TABLE " & strTableName, _
            ConfirmConversions:=False
  oMerger.SuppressBlankLines = False
  oMerger.Destination = wdSendToNewDocument
  oMerger.Execute

  'Save
  oDocument.SaveAs strNewDocumentFileName
  oDocument.Close
End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now