View Single Post
  #7   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

BTW, another thing you /may/ need to take account of in your code is that

dataSrc.RecordCount

does not necessarily return the number of records in the data source.
With some data sources (e.g. a Word document) it may return -1.

Also, depending on what your code needs to achieve, you may have to take
account of the fact that the user can include/exclude individual
records, so that even when RecordCount is not -1, it may not reflect the
count that is to be merged.

You may be able to circumvent that by
a. setting .ActiveRecord to wdFirstRecord
b. setting .ActiveRecord to wdNextRecord until you get an exception
(In VBA, I see

5853 Invalid parameter.

when go try to go past "end of file")


Peter Jamieson

http://tips.pjmsn.me.uk

On 05/03/2010 19:46, Peter Jamieson wrote:[i]
I made some minor changes and am running the code in a DocumentOpen
event, and yes, with some types of data source it does appear to be very
slow. However, it's not obvious what the common factor is - so far I
have tried
a. an Outlook source (i.e. connecting from Word. That would use the
Jet/ACE OLE DB provider and the Outlook/Exchange IISAM. Slow.
b. More or less the same data in a .csv file. In this case, Word would
use the same provider but the Text IISAM (may not mean anything to you).
Slow.

Either way, I see the following message, which doesn't mean much to me
but may mean something to you:

A first chance exception of type
'System.Runtime.InteropServices.SEHException' occurred in WordAddIn3.DLL

c. More or less the same data in a .xls file. In this case, Word would
probably use the same provider but the Excel IISAM. Much quicker!

Not really enugh to draw much of a conclusion except that the type of
data source does seem to make a significant different.



Peter Jamieson

http://tips.pjmsn.me.uk

On 05/03/2010 16:33, amr wrote:[i]

A simple callback for a ribbon button is below. Before you press the
button,
you have to load a source. I loaded my outlook contacts.

public void TestDataSource(Microsoft.Office.Core.IRibbonContro l ribbon) {

Word.MailMergeDataSource dataSrc =
Application.ActiveDocument.MailMerge.DataSource

int count = dataSrc.RecordCount;

if (count 0) {
string[][] addressList = new string[count][5];

Word.MailMergeDataFields fields = dataSrc.DataFields;

object indexLastname = 2;
object indexFirstname = 1;
object indexCompany = 4;
object indexStreet = 8;
object indexZip = 11;
object indexCity = 9;

object index = 0;
for (int i = 0; i count; i++) {
int fieldCount = fields.Count;

addressList[i][0] = (fieldCount= (int)indexFirstname) ?
fields.get_Item(ref indexFirstname).Value + " " + fields.get_Item(ref
indexLastname).Value : "";
addressList[i][1] = (fieldCount= (int)indexCompany) ?
fields.get_Item(ref indexCompany).Value : "";
addressList[i][2] = (fieldCount= (int)indexStreet) ?
fields.get_Item(ref indexStreet).Value : "";
addressList[3] = (fieldCount= (int)indexZip) ?
fields.get_Item(ref indexZip).Value : "";
addressList[4] = (fieldCount= (int)indexCity) ?
fields.get_Item(ref indexCity).Value : "";

dataSrc.ActiveRecord =
Microsoft.Office.Interop.Word.WdMailMergeActiveRec ord.wdNextDataSourceRecord;

}
}

}