I use the following code to merge data from Access into a Word document in which the merge had been set up.
The result is a long list of letters in one Word file.
Is there a way to add code that would save each letter separately?
The file name should be based on two fields from the same query the merge is based on: qryComp.
The fields are [LastName] and [CourseTitle].
Thank you.
Code:
Private Sub Command1_Click()
Dim strPath As String
Dim strDataSource As String
Dim conTemplate As String
Dim conQuery As String
Dim doc As Word.Document
Dim wrdApp As Word.Application
conTemplate = "11 Instructor Contract Template.doc"
conQuery = "qryComp"
On Error GoTo HandleErrors
' Delete the rtf file, if it already exists.
strPath = "H:\"
strDataSource = strPath & conQuery & ".doc"
Kill strDataSource
' Export the data to rtf format.
DoCmd.OutputTo acOutputQuery, conQuery, _
acFormatRTF, strDataSource, False
' Start Word using the mail merge template.
Set wrdApp = New Word.Application
Set doc = wrdApp.Documents.Add(strPath & conTemplate)
' Do the mail merge to a new document.
With doc.MailMerge
.OpenDataSource Name:=strDataSource
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
If .State = wdMainAndDataSource Then
.Execute
End If
End With
' Display the mail merge document.
wrdApp.Visible = True
ExitHere:
Set doc = Nothing
Set wrdApp = Nothing
Set doc2 = Nothing
Set wrdApp2 = Nothing
Exit Sub
HandleErrors:
Select Case Err.Number
Case 53 ' File not found.
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Select
End Sub