This code works very well and does split the files into word and pdf from my access table, however, it groups them into one file.
For example, if I have two names in my table:
John Doe
Jane Doe
It will create a PDF/Word named John Doe and Jane Doe, however, John and Jane are in the same file for each of the mail merges. It seems to be adding tee loop together for the record count. Any Ideas??
here is my code:
For example, if I have two names in my table:
John Doe
Jane Doe
It will create a PDF/Word named John Doe and Jane Doe, however, John and Jane are in the same file for each of the mail merges. It seems to be adding tee loop together for the record count. Any Ideas??
here is my code:
Code:
Private Sub Command1338_Click()
On Error GoTo Err_Command1338
Dim currentDbName As String
Dim strList As String
Dim ObjWordApp As Word.Application
Dim ObjWord As Word.Document
DoCmd.SetWarnings False
strList = Forms![frmSeverance]![Manager]
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
Dim rec, LastRecord As Integer
Dim docNameField, strDocName, strPDFName, savePath As String
If strList = "Test Letter" Then
Set ObjWordApp = CreateObject("Word.Application")
Set ObjWord = ObjWordApp.Documents.Open("C:\TestLetter.docx")
ObjWordApp.Application.Visible = True
ObjWord.MailMerge.OpenDataSource _
Name:=currentDbName, LinkToSource:=True, Connection:="DSN=MS Access Database;" & "DBQ=" & CurrentDb.Name & ";", _
sqlstatement:="SELECT * FROM [tblSeveranceLetters]"
ObjWord.MailMerge.DataSource.ActiveRecord = wdLastRecord
LastRecord = ObjWord.MailMerge.DataSource.ActiveRecord
If MsgBox(LastRecord & " employee letters will be created based on your Mail Merge template.", vbOKCancel) = vbOK Then
savePath = "C:\Completed Letters\"
docNameField = ("Name")
For rec = ObjWord.MailMerge.DataSource.FirstRecord To LastRecord
ObjWord.MailMerge.DataSource.ActiveRecord = rec
strDocName = ObjWord.MailMerge.DataSource.DataFields(docNameField).Value & ".docx"
strPDFName = ObjWord.MailMerge.DataSource.DataFields(docNameField).Value & ".pdf"
With ObjWord.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
ObjWordApp.ActiveDocument.SaveAs Filename:=savePath & strDocName, FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
ObjWordApp.ActiveDocument.SaveAs Filename:=savePath & strPDFName, FileFormat:=wdFormatPDF, AddToRecentFiles:=False
ObjWordApp.ActiveDocument.Close False
ObjWordApp.ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Next rec
Else
Exit Sub
End If