View Single Post
  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
AMR AMR is offline
external usenet poster
 
Posts: 6
Default MailMergeDataSource iteration extremly slow

Hi Peter,
thanks for your investigations.

I dont execute this code during a mailmerge event.
After the mailmerge addresses were selected, the user can open a dialog and
edit a product list. Each address has a product. At dialog initilisation i
load the addresses from the datasource. The source is an outlook addressbook
with two members.
When i try your example code during the mailmerge event , your right, its
pretty fast.

The code below should work after addresses were loaded.

using System.Windows.Data;

[ValueConversion(typeof(Word.MailMergeDataSource), typeof(ListAddress))]
public class MailMergeDataSourceConverter : IValueConverter {

private ListAddress addressList;

#region IValueConverter Member

public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
if (value != null) {
try {
Word.MailMergeDataSource dataSrc =
(Word.MailMergeDataSource)value;

int count = dataSrc.RecordCount;
Log.Info(string.Format("Converting MailMergeDataSource
(Count: {0})", count));

if (count 0) {
addressList = new ListAddress();

Word.MailMergeDataFields fields = dataSrc.DataFields;

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++) {
int fieldCount = fields.Count;

// Crap
string name = (fieldCount = (int)indexFirstname) ?
fields.get_Item(ref indexFirstname).Value + " " + fields.get_Item(ref
indexLastname).Value : "";
string company = (fieldCount = (int)indexCompany) ?
fields.get_Item(ref indexCompany).Value : "";
string street = (fieldCount = (int)indexStreet) ?
fields.get_Item(ref indexStreet).Value : "";
string zip = (fieldCount = (int)indexZip) ?
fields.get_Item(ref indexZip).Value : "";
string city = (fieldCount = (int)indexCity) ?
fields.get_Item(ref indexCity).Value : "";

addressList.Add(new Address() {
Name = name,
Company = company,
Street = street,
Zip = zip,
City = city
});

dataSrc.ActiveRecord =
Word.WdMailMergeActiveRecord.wdNextDataSourceRecor d;
}
}
}
catch (Exception ex) {
Log.Error(ex);
}
}

return addressList;
}

public object ConvertBack(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}

#endregion
}

Maybe it's a matter of interop. I don't really know how this datasource
behaves or works under the hood.