Home |
Search |
Today's Posts |
#1
|
|||
|
|||
MailMergeBeforeRecordMerge cancels entire merge:
Hi group.
I am using the MailMergeBeforeRecordMerge event during a merge, and I am inspecting the data. Everything works fine until I hit a record I don't want to merge, so I set Cancel = True and it stops the entire merge, not just the one record. I am using another word doc as a data source. Any ideas why this would happen? Thanks _Randal |
#2
|
|||
|
|||
It isn't what (usually!) happens here
Are you definitely only setting Cancel to True for the one record? Is it only happening on one particular merge or is it happening on every merge? Is your Event handling code written in VBA or in a .NET language? Peter Jamieson wrote in message oups.com... Hi group. I am using the MailMergeBeforeRecordMerge event during a merge, and I am inspecting the data. Everything works fine until I hit a record I don't want to merge, so I set Cancel = True and it stops the entire merge, not just the one record. I am using another word doc as a data source. Any ideas why this would happen? Thanks _Randal |
#3
|
|||
|
|||
Are you definitely only setting Cancel to True for the one record?
Is it only happening on one particular merge or is it happening on every merge? Is your Event handling code written in VBA or in a .NET language? Well word is being instantiated, the merge template opened and the datasource document created from a C# class I wrote. The event handling code is called through a macro written in VBA in the merge template document itself. When I set a break point in the MailMergeBeforeRecordMerge event handler and step through it works and fires each time, after it hits the line setting Cancel = True the entire merge is aborted. I suppose I am only setting Cancel true for one record since it is the current record context that the event gets called right? I only see one Cancel parameter in the event handling routine. Here is the code if that will shed any light: Private Sub MailMergeApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean) Dim mergeDataTable As Table Dim mergeData As MailMergeDataSource If EventId CInt(Doc.MailMerge.DataSource.DataFields(8).Value) Then EventId = CInt(Doc.MailMerge.DataSource.DataFields(8).Value) Cancel = False Else Cancel = True End If If ActiveDocument.Tables.Count 0 Then Set mergeDataTable = ActiveDocument.Tables(1) Set mergeData = Doc.MailMerge.DataSource newrow = mergeDataTable.Rows.Add() mergeDataTable.Cell(mergeDataTable.Rows.Count, 1).Range.InsertAfter (mergeData.DataFields(9).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 2).Range.InsertAfter (mergeData.DataFields(10).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 3).Range.InsertAfter (mergeData.DataFields(11).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 4).Range.InsertAfter (mergeData.DataFields(12).Value) End If End Sub |
#4
|
|||
|
|||
The reason I asked about the language etc. is because when I tried using
Mail Merge events from VB.NET I did experience some very strange results. However, I'm still not particularly familiar with that environment so may not be able to help. The other question I have, however, is: does the same problem occur if you actually open the same Word document manually (i.e. avoiding all the .net stuff), connect the same data source, and perform the merge? Peter Jamieson wrote in message oups.com... Are you definitely only setting Cancel to True for the one record? Is it only happening on one particular merge or is it happening on every merge? Is your Event handling code written in VBA or in a .NET language? Well word is being instantiated, the merge template opened and the datasource document created from a C# class I wrote. The event handling code is called through a macro written in VBA in the merge template document itself. When I set a break point in the MailMergeBeforeRecordMerge event handler and step through it works and fires each time, after it hits the line setting Cancel = True the entire merge is aborted. I suppose I am only setting Cancel true for one record since it is the current record context that the event gets called right? I only see one Cancel parameter in the event handling routine. Here is the code if that will shed any light: Private Sub MailMergeApp_MailMergeBeforeRecordMerge(ByVal Doc As Document, Cancel As Boolean) Dim mergeDataTable As Table Dim mergeData As MailMergeDataSource If EventId CInt(Doc.MailMerge.DataSource.DataFields(8).Value) Then EventId = CInt(Doc.MailMerge.DataSource.DataFields(8).Value) Cancel = False Else Cancel = True End If If ActiveDocument.Tables.Count 0 Then Set mergeDataTable = ActiveDocument.Tables(1) Set mergeData = Doc.MailMerge.DataSource newrow = mergeDataTable.Rows.Add() mergeDataTable.Cell(mergeDataTable.Rows.Count, 1).Range.InsertAfter (mergeData.DataFields(9).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 2).Range.InsertAfter (mergeData.DataFields(10).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 3).Range.InsertAfter (mergeData.DataFields(11).Value) mergeDataTable.Cell(mergeDataTable.Rows.Count, 4).Range.InsertAfter (mergeData.DataFields(12).Value) End If End Sub |
#6
|
|||
|
|||
I got it worked out, thanks anyway!
_Randal |
#7
|
|||
|
|||
Glad it works - can you tell us what was wrong or made it work?
Peter Jamieson wrote in message oups.com... I got it worked out, thanks anyway! _Randal |
#8
|
|||
|
|||
Advancing the datasource to the next record while in the
MailMergeBeforeRecordMerge event causes word to fire the next merge, but it doesn't appear sophisticated enough to realize that it's calling itself recursively. Sort of like this: 1) I am in the MailMergeBeforeRecordMerge property and I programmatically advance to the next record in the dataset 2) Word automatically tries to do the merge when that is called, but when in debug mode it doesn't re-enter the MailMergeBeforeRecordMerge event in a nested manner - or at least it doesn't let you see it. 3) Cancel seems to be a byref variable, so I think when it goes into it's mysterious recursion into MailMergeBeforeRecordMerge I believe that variable is now set to true for each recursive instance into MailMergeBeforeRecordMerge and had the effect of cancelling the entire merge. The solution was to slightly alter the code and place it in the MailMergeAfterRecordMerge event, and have a global variable control whether to cancel the MailMergeBeforeRecordMerge event - I posted the code in a different thread for someone who was in a similar plight as mine. This approach works great. _Randal |
#9
|
|||
|
|||
Thanks.
In fact I'd had the same problem changes to ActiveRecord causing VBA to take weird paths through the code, but could not see where you were making such a change in the code you originally posted. Having a workaround for this really makes the MailMerge events a whole lot more useful, so thanks very much for posting your other bit of code. Peter Jamieson wrote in message oups.com... Advancing the datasource to the next record while in the MailMergeBeforeRecordMerge event causes word to fire the next merge, but it doesn't appear sophisticated enough to realize that it's calling itself recursively. Sort of like this: 1) I am in the MailMergeBeforeRecordMerge property and I programmatically advance to the next record in the dataset 2) Word automatically tries to do the merge when that is called, but when in debug mode it doesn't re-enter the MailMergeBeforeRecordMerge event in a nested manner - or at least it doesn't let you see it. 3) Cancel seems to be a byref variable, so I think when it goes into it's mysterious recursion into MailMergeBeforeRecordMerge I believe that variable is now set to true for each recursive instance into MailMergeBeforeRecordMerge and had the effect of cancelling the entire merge. The solution was to slightly alter the code and place it in the MailMergeAfterRecordMerge event, and have a global variable control whether to cancel the MailMergeBeforeRecordMerge event - I posted the code in a different thread for someone who was in a similar plight as mine. This approach works great. _Randal |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Multiple merge with IncludeText generating extraneous hidden chara | Mailmerge | |||
mail merge with attachments | Mailmerge | |||
Word Compare and Merge Document Function | Microsoft Word Help | |||
Can you create a multi-layered merge where certain merge fields a. | Mailmerge | |||
stripping commas from merge data | Mailmerge |