View Single Post
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default MailMergeDataSource iteration extremly slow

I created a very small VS2008 Addin with the following code as a test:

public partial class ThisAddIn
{
private Word.Application oWord;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
oWord = this.Application;
oWord.MailMergeBeforeRecordMerge +=
new Word.ApplicationEvents4_MailMergeBeforeRecordMerge EventHandler(
oWord_MailMergeBeforeRecordMerge);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

private void
oWord_MailMergeBeforeRecordMerge(Microsoft.Office. Interop.Word.Document
doc, ref bool Cancel)
{
for (int i = 1; i = doc.MailMerge.DataSource.DataFields.Count; i++)
{
object j = i;

doc.Content.InsertAfter(doc.MailMerge.DataSource.D ataFields.get_Item(ref
j).Value);
}
}

It seems to run pretty fast (small Excel data source with 4 columns). I
wouldn't execute anything like
dataSrc.ActiveRecord = Word.WdMailMergeActiveRecord.wdNextDataSourceRecor d
within that because when you're using MailMerge Events you shouldn't
move records within the event handler because Word tends to get
confused. So I hope you aren't doing that!

However, assuming that you aren't, my next best guess would be that it's
moving from record to record that's causing the problem. That might be
the case if you are using a data source that Word needs to open with one
of its internal/external converters, e.g. a Word document, text file
with more than 255 fields, etc. But that's not what you're saying, so
I'm stuck.

If you can provide a complete, working chunk of code it would be easier
to check whether it is slow here.

Peter Jamieson

http://tips.pjmsn.me.uk

On 04/03/2010 11:40, amr wrote:
object indexLastname = MailMergeDataFieldIndices.Lastname;
object indexFirstname = MailMergeDataFieldIndices.Firstname;
object indexCompany = MailMergeDataFieldIndices.Company;
object indexStreet = MailMergeDataFieldIndices.Street;
object indexZip = MailMergeDataFieldIndices.Zip;
object indexCity = MailMergeDataFieldIndices.City;

object index = MailMergeDataFieldIndices.None;
for (int i = 0; i count; i++) {
string name = fields.get_Item(ref indexFirstname).Value + " " +
fields.get_Item(ref indexLastname).Value;
string company =fields.get_Item(ref indexCompany).Value;
string street =fields.get_Item(ref indexStreet).Value;
string zip = fields.get_Item(ref indexZip).Value;
string city = fields.get_Item(ref indexCity).Value;

dataSrc.ActiveRecord =
Word.WdMailMergeActiveRecord.wdNextDataSourceRecor d;