Reply
 
Thread Tools Display Modes
  #1   Report Post  
 
Posts: n/a
Default 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   Report Post  
Peter Jamieson
 
Posts: n/a
Default

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   Report Post  
 
Posts: n/a
Default

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   Report Post  
Peter Jamieson
 
Posts: n/a
Default

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



  #5   Report Post  
 
Posts: n/a
Default

Yes it does, that's how I've been testing it.

_Randal



  #6   Report Post  
 
Posts: n/a
Default

I got it worked out, thanks anyway!

_Randal

  #7   Report Post  
Peter Jamieson
 
Posts: n/a
Default

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   Report Post  
 
Posts: n/a
Default

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   Report Post  
Peter Jamieson
 
Posts: n/a
Default

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

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple merge with IncludeText generating extraneous hidden chara Judy Mailmerge 4 May 5th 05 03:51 PM
mail merge with attachments AS Mailmerge 5 April 9th 05 09:49 AM
Word Compare and Merge Document Function EricM Microsoft Word Help 2 March 28th 05 11:53 PM
Can you create a multi-layered merge where certain merge fields a. mileszat Mailmerge 3 January 18th 05 03:46 AM
stripping commas from merge data Patrick Heilman Mailmerge 0 December 30th 04 04:09 AM


All times are GMT +1. The time now is 05:20 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"