merge a 1600 record datasource into 1600 separate documents

J

Jessica

I am trying to merge a 1600 record source into 1600 separate documents. I
don't want the end result to be 1 document to be with 1600 pages but 1600
separate documents. Has anyone ever done this or do you know if it can be
done?
 
D

Doug Robbins

If you are not particular about the file names given to each document, then
use:

Sub splitter()

' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmerge
as a separate file.

Dim i As Long, Source as Document, Target as Document, Letter as Range
Set Source = ActiveDocument
For i = 1 to Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End=Letter.End-1
Set Target = Documents.Add
Target.Range=Letter
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i

End Sub

If you want to save each document with a specific filename that comes from
the data source, then here's a method that I have used that involves
creating a separate catalog type mailmerge maindocument which creates a word
document containing a table in each row of which would be your data from the
database that you
want to use as the filename.

You first execute that mailmerge, then save that file and close it. Then
execute the mailmerge that you want to create the separate files from and
with the result of that on the screen, run a macro containing the following
code
and when the File open dialog appears, select the file containing the table
created by the first mailmerge

' Throw Away Macro created by Doug Robbins
'
Dim Source As Document, oblist As Document, DocName As Range, DocumentName
As String
Dim i As Long, doctext As Range, target As Document
Set Source = ActiveDocument
With Dialogs(wdDialogFileOpen)
.Show
End With
Set oblist = ActiveDocument
Counter = 1
For i = 1 To oblist.Tables(1).Rows.Count
Set DocName = oblist.Tables(1).Cell(i, 1).Range
DocName.End = DocName.End - 1

'Change the path in the following command to suit where you want to save
the documents.
DocumentName = "I:\WorkArea\Documentum\" & DocName.Text
Set doctext = Source.Sections(i).Range
doctext.End = doctext.End - 1
Set target = Documents.Add
target.Range.FormattedText = doctext
target.SaveAs FileName:=DocumentName
target.Close
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
D

DA

Hi Jessica

Here's another thing you could try.
Setup your main merge document (data source, fields etc),
then place this routine in it and run the macro.

--------------
Sub MergeToDocs()
Dim lngCounter As Long

With ActiveDocument.MailMerge
For lngCounter = 1 To .DataSource.RecordCount
.Destination = wdSendToNewDocument
With .DataSource
.FirstRecord = lngCounter
.LastRecord = lngCounter
End With
.Execute
With ActiveDocument
.SaveAs FileName:=.FullName
.Close
End With
Next lngCounter
End With
End Sub
 
J

Jessica

I think this is very helpful but I don't quite understand the second part.
That is what I am more interested in.

Can you please try to make it a little more clear for me?

So do I run my main mail merge first then run the first macro to get it
split out? I am confused?
 
M

Malcolm Smith

Indeed, it can be done. But I wouldn't call it a 'merge'.

Let's imagine that this is a letter going out to people. You have a
template with the text in and a bookmark where each of the record's fields
go. These could be, say, the Name and Address and the Salutation.

So you have a loop which goes through the recordset one at a time.

Each time it goes through the recordset it creates a new document based on
that template and then puts the contents of the fields into the bookmark's
text area;

oDoc.Bookmarks("bmkSalutation").Range.Text = oRS("Salutation")

and so on.

Print out the document and then close it without saving.

I usually have a five second pause or something in here so that the
printer server doesn't explode.

Then do the next one.

Then take the whole lot off the printer and apply envelopes and postage
stamps.


I do something similar with my eMail newsletters.

- Malc
www.dragondrop.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top