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?
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