Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Biz Analyst Biz Analyst is offline
external usenet poster
 
Posts: 2
Default Saving a mail merged document using the value from a datafield

Hi,

I am running a mail merge to a new document. The template is one
page, and I have some code to pull each page from the merged document
and save it as its own new file. So, right now, it ends up being
MergeResult1.doc, MergeResult2.doc, MergeResult3.doc, etc... What I
want to do is save each file using one of the datafield values
(employee ID). In this case, it would look like this:
employee_12345.doc, employee_67890.doc, employee_98765.doc, etc.
Right now the code saves the doc using a doc counter, but I want to
have it pull the value from the employee ID datafield and add it to
the filename string. I know this is simple for someone out there.
Can you help me out? ~ Sean


Here's my code


Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
..SaveAs FileName:="MergeResult" & CStr(docCounter)
..Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub

  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Saving a mail merged document using the value from a datafield

It's actually a bit easier if you can pick up the value of the merge field
during the merge - after you have done the merge, the association between
the sections of the output document and the data source is in essence gone,
so you would have to know precisely where to look for the employee ID within
the output document.

In simple cases (where there is a one-to-one correspondence between a
record in the data source and an output letter), you can use code like the
following:

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 -
' and make sure the merge field name is identical
' (here, it is case sensitive)

strOutputDocumentName = _
"c:\mydoc\MergeResult_" & _
trim(cstr(.DataSource.Datafields("employee ID").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


Other approaches are described on Graham Mayor's site at

http://www.gmayor.com/individual_merge_letters.htm

Peter Jamieson


"Biz Analyst" wrote in message
ps.com...
Hi,

I am running a mail merge to a new document. The template is one
page, and I have some code to pull each page from the merged document
and save it as its own new file. So, right now, it ends up being
MergeResult1.doc, MergeResult2.doc, MergeResult3.doc, etc... What I
want to do is save each file using one of the datafield values
(employee ID). In this case, it would look like this:
employee_12345.doc, employee_67890.doc, employee_98765.doc, etc.
Right now the code saves the doc using a doc counter, but I want to
have it pull the value from the employee ID datafield and add it to
the filename string. I know this is simple for someone out there.
Can you help me out? ~ Sean


Here's my code


Sub SaveAllSubDocs(ByRef doc As Word.Document)
Dim subdoc As Word.Subdocument
Dim newdoc As Word.Document
Dim docCounter As Long
docCounter = 1
'Must be in MasterView to work with
'Subdocs as separate files
doc.ActiveWindow.View = wdMasterView
For Each subdoc In doc.Subdocuments
Set newdoc = subdoc.Open
'Remove NextPage section breaks
'originating from mailmerge
RemoveAllSectionBreaks newdoc
With newdoc
.SaveAs FileName:="MergeResult" & CStr(docCounter)
.Close
End With
docCounter = docCounter + 1
Next subdoc
End Sub


Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a mail merged document using the value from a datafield Biz Analyst Microsoft Word Help 1 July 6th 07 05:56 AM
Saving mail merged letters as seperate documents Nayme Microsoft Word Help 2 March 13th 07 04:15 PM
Problem Saving a Merged Document in Word 2003 Tim Mailmerge 1 March 9th 07 04:32 AM
E-mail merged document with replies going to different e-mail addr Beach Lover Mailmerge 2 February 23rd 07 07:06 PM
How do I attach a WORD document to mail-merged e-mail? Mlou Mailmerge 1 November 25th 05 09:41 PM


All times are GMT +1. The time now is 01:34 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"