Word Mail Merge Save Individual Files

marka87uk

Board Regular
Joined
Mar 24, 2007
Messages
247
Hi,

I have a macro in Word which loops through each mail merge record and saves a copy of the file with that record only.

The problem I have is it seems to loop through all records whether they are selected in the mail merge or not (the spreadsheet it links to is large and this takes a long time) but it saves only the selected records correctly. This means we have to force close Word after the individual files have been created.

Please have a look at the code below and let me know if there is something I can change to get it working correctly?

Code:
Sub SaveLeaverDetails()
Dim i As Long
i = ActiveDocument.MailMerge.DataSource.RecordCount
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
Do While intCounter < i
intCounter = intCounter + 1
        ' Copy the active record
        ActiveDocument.Range.Copy
        ' Create a new document and paste the record
        Documents.Add
        Selection.Paste
        ' Set the open directory where the file is to be saved
        ChangeFileOpenDirectory _
        "Y:\"
        ' Remove the mail merge from the document
        ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
        ' Select all and replace all fields with their text values
        Selection.WholeStory
        Selection.Fields.Unlink
        ' Save the file using bookmark names within the file name
        ActiveDocument.SaveAs FileName:="Leaver Details - " & _
        ActiveDocument.Bookmarks("branch_number").Range.Text & " - " & _
        ActiveDocument.Bookmarks("payroll_number").Range.Text & ".doc", _
        ReadOnlyRecommended:=True
        ' Close the document
        ActiveDocument.Close
        
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
Loop
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I've realised the problem is that 'ActiveDocument.MailMerge.DataSource.RecordCount' returns the total number of mail merge records - not just the ones selected.

Is there a method to count the number of selected records only?
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top