MailMergeBeforeRecordMerge cancels entire merge:

F

frandalc

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
 
P

Peter Jamieson

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
 
F

frandalc

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


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
 
P

Peter Jamieson

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
 
P

Peter Jamieson

Glad it works - can you tell us what was wrong or made it work?

Peter Jamieson
 
F

frandalc

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
 
P

Peter Jamieson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top