Creating Separate Word Documents in Word 2002

  • Thread starter Patricia Morrigan
  • Start date
P

Patricia Morrigan

Hello everyone,

I'm only new to News Groups but I have been avidly reading all the
recent solutions to this problem (see subject). Recently I used Doug
Robbins' 'Splitter' macro in my merged Word 2002 (in Win XP
environment)document. It has been very useful (thankyou Doug!) but I
would like it to do a few more things. My husband has been trying to
alter the code (unsuccessfully) to:

1)Capture the Merge Fields (First Name & Last Name) so that they can
be used in the name of the output documents. In the 'Splitter' code
the System Date and a Counter are used to create each separate
document name. However, I would like to use (in my case) the student
names as an identifier of the document by using the First Name & Last
Name fields (i.e. Bill Smith becomes Bill Smith.doc)

2)The 'Splitter' code separates the text by copying the text and each
Section Break and then pasting into a new document. That is fine but
I would like it to remove the Section Break from each new document
because I don't want two pages for each new document (the section
break causes this).

It would be much appreciated if anyone has the relevant solution(s) to
this particular problem.

Thankyou Patricia
 
G

Graham Mayor

Once you complete the merge the fields no longer exist in the target
document - so you can't use them in the way you envisage, however, see if my
reply this morning to gellione who has a similar issue, helps.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter Jamieson

Another approach you could try is to use a macro to perform one merge for
each record in the data source. But as it stands, this macro will only work
if you don't use SKIP/NEXT type fields in your mail merge main document.

Sub ProduceOneDocPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.

Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim strOutputDocumentName As String
Dim TerminateMerge As Boolean


' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' If no data source has been defined, do it here using OpenDataSource.
' But if it is already defined in the document, you should not need to
define it here.

' .OpenDataSource _
' Name:="whatever"

intSourceRecord = 1
TerminateMerge = False

Do Until TerminateMerge
.DataSource.ActiveRecord = intSourceRecord

' if we have gone past the end (and possibly, if there are no records)
' then the Activerecord will not be what we have just tried to set it to

If .DataSource.ActiveRecord <> intSourceRecord Then
TerminateMerge = True
' the record exists
Else

' while we are looking at the correct activerecord,
' create the document path name
' e.g. - you will need to change this -
strOutputDocumentName = "c:\mydoc\" &
..DataSource.Datafields("Partner_Name").Value &
".doc"

.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToNewDocument
.Execute

' The Activedocument is always the output document
' Add any parameters you need to these calls
ActiveDocument.SaveAs strOutputDocumentName
ActiveDocument.Close
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub
 
D

Doug Robbins

Hi Patricia,

Here's another 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
 
G

Graham Mayor

I think we may be getting somewhere. This gave me a clue as to how to set
the splitter macro to work using only the one merge document. It entails
adding a redundant field to the very start of the merge document that will
contain the information to be used in the filename. In this instance I have
just used a single word, though it would be simple to change that if
necessary. The macro then grabs the first word in the document - the name -
and deletes it before saving - here in the format "Name 050804 1.doc"

Sub Splitter()
' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each letter created by a
' mailmerge as a separate file.
' With minor modifications by Graham Mayor 10-02-03& 04-08-2004

Dim mask, docName, sName As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "ddMMyy"
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
sName = Selection
Selection.Delete Unit:=wdCharacter, Count:=1
Docname = "D:\My Documents\Test\Merge\" _
& sName & " " & Format(Date, mask) & " " & LTrim$(Str$(Counter))
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=Docname, _
FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Application.ScreenUpdating = True
Wend
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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