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 & Print events

There does not seem to be an "DocumentAfterPrint" event ? I assume this is
because I suppose you can't tell when a document is "actually" printed
with buffers/network printers etc etc


I don't know, but that would be my guess too (NB, the people here are mainly
volunteers who don't work for Microsoft, so you are unlikely to get
definitive information on why things were designed a particular way).

You can use a macro like the following to perform the merge, but only if
your mail merge main document does not have any Next record fields and
similar stuff such as Next record if . NB, the loop is more complicated
than a simple for...next simply because I have found that the record numbers
are not necessarily what you might expect in some cases.

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 strEman As String
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 -
strEman = StrReverse(objMerge.Name)
StrReverse(Mid(strEman, InStr(1, strEman & ".", ".") + 1))

strOutputDocumentName = _
"c:\mydoc\" & _
StrReverse(Mid(strEman, InStr(1, strEman & ".", ".") + 1)) & _
.DataSource.Datafields("CaseID").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


"Andrew Kennard" wrote in message
...
Hi all

I would like to be able to automate a mail merge of records/documents one
by one so that I can get notifications of things happening

I see in the Word 2003 Object model there are events for
MailMergeAfterMerge and DocumentBeforePrint

I don't want the whole process to be too slow but I really need to kepp
tight control on which merged documents have/havn't been printed.

There does not seem to be an "DocumentAfterPrint" event ? I assume this is
because I suppose you can't tell when a document is "actually" printed
with buffers/network printers etc etc

Does anyone have a "one merge document at a time" example to give me a
head start ?

When you start to do merges from larger source data say 10,000+ records
you really need tight control incase of jams etc etc to make it easier to
restart and keep track of things etc

Thanks in advance for any advise

Kind Regards

Andrew