![]() |
|
#1
|
|||
|
|||
|
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
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Unexpected Big Problem With Mail Merge via Excel VBA - DataLink Problem
|
JennEx | Mail Merge | 6 | 05-26-2019 06:08 AM |
| vba error when trying to do a mail merge | texas1014 | Mail Merge | 1 | 03-08-2017 03:23 PM |
| Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error | zipit189 | Word VBA | 7 | 03-18-2015 01:13 PM |
| time field issue in mail merge | Aude | Mail Merge | 16 | 05-18-2014 06:22 AM |
Error 5631 in Mail Merge
|
DoctorNadir | Mail Merge | 7 | 12-16-2011 04:38 PM |