|
|
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
|
|
|
[Quoted Text] > 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" <b[ at ]a.com> wrote in message news:eN9kMDikHHA.1992[ at ]TK2MSFTNGP05.phx.gbl... > 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 > > > > > >
|
|
Peter
Thanks very much for your detailed reply and example which is very useful.
Could you expand on "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." ?
Your loop seems to terminate when an unexpected record is found. Is it just that when you 'fall off the end' the contents of the 'next record' can be unpredictable ?
A 2nd question. I'm surprised how few events Word has for helping with automation. One other thing I need to do is get the name of the document that has just been saved by the user in a SaveAs ie they typed it in so I don't know what it is. I'm currently looking at the WindowDeactivate event as this seems to fire pretty late in the order of things. Have you done anything similar ? to get the Document.Name and Document.Path back into your application so that you can do something with it. ?
Thanks again
Andrew
"Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> wrote in message news:%23uDkEMjkHHA.3512[ at ]TK2MSFTNGP06.phx.gbl...
[Quoted Text] >> 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" <b[ at ]a.com> wrote in message > news:eN9kMDikHHA.1992[ at ]TK2MSFTNGP05.phx.gbl... >> 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 >> >> >> >> >> >> > >
|
|
To be honest, I can't reemember, as it's a long time since I wrote this, but I think it was actually to do with one or both of the folowing a. Lastrecord not always being filled in correctly, so you can't just do For myrec = 1 to .Lastrecord b. user selections creating gaps in the record sequence that aren'y actually reflected in the value of Lastrecord.
[Quoted Text] > A 2nd question. I'm surprised how few events Word has for helping with > automation. One other thing I need to do is get the name of the document > that has just been saved by the user in a SaveAs ie they typed it in so I > don't know what it is.
You probably need to use the built-in SaveAs dialog - see for example http://word.mvps.org/FAQs/MacrosVBA/ChangeSaveAsPath.htm
Peter Jamieson
"Andrew Kennard" <b[ at ]a.com> wrote in message news:OE$ZMejkHHA.492[ at ]TK2MSFTNGP04.phx.gbl... > Peter > > Thanks very much for your detailed reply and example which is very useful. > > Could you expand on "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." ? > > Your loop seems to terminate when an unexpected record is found. Is it > just that when you 'fall off the end' the contents of the 'next record' > can be unpredictable ? > > A 2nd question. I'm surprised how few events Word has for helping with > automation. One other thing I need to do is get the name of the document > that has just been saved by the user in a SaveAs ie they typed it in so I > don't know what it is. I'm currently looking at the WindowDeactivate event > as this seems to fire pretty late in the order of things. Have you done > anything similar ? to get the Document.Name and Document.Path back into > your application so that you can do something with it. ? > > Thanks again > > Andrew > > > "Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> wrote in message > news:%23uDkEMjkHHA.3512[ at ]TK2MSFTNGP06.phx.gbl... >>> 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" <b[ at ]a.com> wrote in message >> news:eN9kMDikHHA.1992[ at ]TK2MSFTNGP05.phx.gbl... >>> 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 >>> >>> >>> >>> >>> >>> >> >> > >
|
|
|