-Jas Guest
|
Posted: Fri Aug 05, 2005 1:40 am Post subject: Word event not firing consistantly in MailMerge |
|
|
Delphi 7, XP pro, Word 2003
I have found that the events during Word MailMerge don't always fire and I'm hoping someone here can tell me why, and what to do about it.
When doing a mailmerge from Delphi I have created routines for the two events, before and after record merge.
They both fire all of the time when I do a merge of about 40 records in a single pass as follows:
I dropped the Delphi component for WordApplication onto the form and that is what I'm referncing below.
WordApp1.Documents.Open(DocFilename, EmptyParam, ....etc)
WordApp1.ActiveDocument.MailMerge.OpenDataSource(DataFilename, EmptyParam, ....etc)
WordApp1.ActiveDocument.MailMerge.Destination := wdSendToPrinter
WordApp1.ActiveDocument.MailMerge.Execute(bTrue);
WordApp1.ActiveDocument.Close(DoNotSaveChanges,EmptyParam,EmptyParam);
However, if I do a calling the above 20 times for a single document, the event MailMergeAfterRecordMerge at random, skips firing.
Somebody was kind enough to point out that events in general are queued up and will fire earlier or later depending on what's already in the queue. So I put in about a dozen application.processmessages between each of the above steps and it works without a skip.
The downside of course is that the process really runs slowly.
So I tried something else (totally my own stupid idea).
In the event code I increment a counter. If the event doesn't fire, then my counter doesn't get incremented.
I thought the problem was in closing before the event could fire, so I put a loop right after the call to Execute
and tested my counter until it had incremented.
i := iCounter
x := 0;
WordApp1.ActiveDocument.MailMerge.Execute(bTrue);
while i = iCounter do
begin
application.processmessages;
inc(x);
end;
WordApp1.ActiveDocument.Close(DoNotSaveChanges,EmptyParam,EmptyParam);
What I found suprised me: if the event had not fired by the time it returned from the Execute call, it wasn't going to, no matter how long I waited.
So my question, can I force the event to fire? or failing that how can I wait the minimum time so that it does fire?
Any insight that might help here would be greatly appreciated.
Thanks,
-Jas
|
|