Hi,
I create an Access database as data source. The mail merge grabs the data from the database and feeds into a letter. The macro, which I found online, only works when all entries are selected. It runs fine and splits out each letter with a pre-defined file name. However, when I only select few entries (like entry 2, 5, 9, 11), it gives me a run time error 5631.
VBA Error Screenshot
Below is my VBA code
Code:
Sub SaveAsFileName()
Dim FileName As String
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
For SectionCount = 1 To .DataSource.RecordCount
With .DataSource
ActiveDocument.MailMerge.DataSource.ActiveRecord = SectionCount
ActiveDocument.MailMerge.DataSource.FirstRecord = SectionCount
ActiveDocument.MailMerge.DataSource.LastRecord = SectionCount
'replace Filename with the column heading that you want to use - can't have certain symbols in the name
FileName = .DataFields("Letter").Value & "_" & .DataFields("Name").Value
End With
'Get path and file name
FullPathAndName = ActiveDocument.Path & Application.PathSeparator & FileName & ".docx"
'Merge the document
.Execute Pause:=False
'Save resulting document.
ActiveDocument.SaveAs (FullPathAndName)
ActiveDocument.Close False
Next
End With
End Sub