![]() |
|
|
|
#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
|
|
#2
|
||||
|
||||
|
How are you 'selecting' the records to merge?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
I select them via Microsoft Word - Mailings - Recipent List
|
|
#4
|
||||
|
||||
|
Provided you've unchecked all records, then checked the one(s) you want, there shouldn't be a problem - unless you also have a filter that results in your checked records being filtered out.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
I cleared all selected records and selected a few. That's when the error occurred when I run the macro. There are no filter on my data at all.
|
|
#6
|
||||
|
||||
|
Try the macro under the topic Send Mailmerge Output to Individual Files in the Mailmerge Tips & Tricks 'Sticky' thread at the top of this forum: https://www.msofficeforums.com/mail-...ps-tricks.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
It works, but I can't seem get the code to save merged files into specified folder and not to preview any merged letters. It currently shows all merged letters as individual files each time I run this code and it does not auto close. Any idea?
|
|
#8
|
||||
|
||||
|
Quote:
The code does not show the merged letters - it simply saves them. Only the mailmerge main document remains open.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
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 |