View Single Post
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Mailmerge and VBA Question

It isn't exactly what you describe, but I suggest you have a look at

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

The following is my own old macro that does one merge per source record. It
won't work if the merge consumes multiple records per merge. You'll need to
modify the code that sets up the output document name, and probably debug
the thing (sorry!)

Peter Jamieson

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("myfield").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


"jim" wrote in message
...
I work for a business that uses third-party software to merge data
from our customer database into individual letters in Word 2000. I
create a main document as a form letter, insert all of the appropriate
merge fields and identify the main document to the software. I do not
actually perform the mailmerge in Word; the third party software finds
the proper customer record, does the merge, and I get a new document
that is merged with all the data in the proper spots and in all
respects looks just like any formletter that I could merge through
Word 2000.

After the merge happens and I get a newly merged document, I would
like the user to be able to run a VBA macro that will allow the user
to save the new document with a filename that is based on the contents
of several of the mergefields. However, after the merge

ActiveDocument.MailMerge.DataSource.DataFields(ind ex).Value

gives me a runtime error 5852 "Requested object not available" for any
correct value of index and therefore I have no data to massage into a
properly constituted filename. If I try to access this before the
merge is done, I have values in each of the DataFields and I can
massage that data in any way necessary to create a filename, but I
don't know how to save it for after the merge or how to pass it
through the merge to the other side so that it exists after the merge.

I had originally tried to set the title property in the merge document
in this manner {TITLE "{MERGEFIELD f15}{MERGEFIELD f12 \@ "' exam'
M-d-yyyy"}"} so that I could access it through using
ActiveDocument.BuiltInDocumentProperties(wdPropert yTitle), but setting
the title that way does not always work for me and I don't need the
title of the document showing up anywhere in the text, which happens
by using the {TITLE} field

How can I (or can I at all) carry the contents of the data fields that
I need to create my properly constituted filename past the mailmerge
process, or in the alternative, how and where can I save it
"pre-merge" so that I can use it "post-merge?"

Any hope for me?

jim