Saving separated mail merge files, with data in datasource as name !!!!!!!

V

Van123

Hi All

I hope someone can help me with a problem, that has been driving me
mad !!

I created a macro, which spits a mail merge file, into separate
pages...ie if there were 24 pages in the mail merge document, the
macro will split the document into 24 separate docs, and save them to
a folder. Until now, I have not been concerned about the names
assigned to the files, and so used the line of code:
ActiveDocument.SaveAs FileName:="course details_" & DocNum & ".doc"

Now however, I wish to assign a name to the newly separated file with
info from one of the fields from the datasource, so that each filename
is specific to info contained in it. I would like the name of the
file to be the employee no e.g 1048.doc.

I have not created a macro to perform the mail merge itself, perhaps i
need this ??

This is the code so far ....just to split the mailmerge doc .....

Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage

For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of
Pages")

'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy

' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.Paste
Selection.TypeBackspace
ChangeFileOpenDirectory "C:\Training"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="course details_" & DocNum &
".doc"
ActiveDocument.Close

' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

Please someone help me !!!!!!
 
P

Peter Jamieson

Now however, I wish to assign a name to the newly separated file with
info from one of the fields from the datasource, so that each filename
is specific to info contained in it. I would like the name of the
file to be the employee no e.g 1048.doc.

I have not created a macro to perform the mail merge itself, perhaps i
need this ??

You have to get the correct value from somewhere. But after the merge has
completed, the output document is not connected to the data source. So...
a. if you happened to put the value somewhere in the text on each page in a
way that it was easy to locate, you could use VBA to extract it.
b. you could consider connecting the output document to the same data
source, and stepping through the records using the <ailMerge.DataSource
object
c. (probably the simplest approach if there is always one page for each
record in the data source), instead of doing the merge then splitting the
document, you could do one merge for each record in the data source. Like
this (this may fail for large numbers of records):

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.
strOutputDocumentName = _
"c:\Training\course details_" & _
.DataSource.Datafields("Employee_no").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 - Word MVP

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

Dim Source As Document, oblist As Document, DocName As Range, DocumentName
As String
Set Source = ActiveDocument
With Dialogs(wdDialogFileOpen)
.Show
End With
Set oblist = ActiveDocument
Counter = 1
While Counter < oblist.Tables(1).Rows.Count
Set DocName = oblist.Tables(1).Cell(Counter, 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
Source.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs filename:=DocumentName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Counter = Counter + 1
Wend


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - 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