PDA

View Full Version : Access MailMerge DataSource in VBA



advin
01-04-2010, 03:07 AM
Hallo,

I'm trying to acces the DataSource via
Code:
Doc.MailMerge.DataSource.DataFields(INDEX/FIELDNAME).Value
usually this is no problem, but in case of two fields i get an error stating that the memory is corrupt/ or an Automation error!

I have no problems accessing the Name property via: Code:
Doc.MailMerge.DataSource.DataFields(INDEX/FIELDNAME).Name
!

Any ideas what wrong with those two fields?

p.s.: when running the merge ALL fields are populated correctly! Only accessing the fields via VBA is causing an error!

Here's the detailed error message:

"Runtimeerror '-2147417848 (80010108)':

Automation error
The called object was disconnected from the clients"

<- i translated it on my own as i recieve a german error!

regards, advin

TonyJollans
01-04-2010, 10:23 AM
One possible cause of this is implicit instantiation caused by improperly qualified references used with early binding. Without seeing more of your code I can't suggest where this might be.

advin
01-05-2010, 03:44 AM
Hallo,

it's actually quite simple to reproduce

attached you can find a template and and xls- datasource.
Please perform the following steps:
1; Open template
2; choose the mailmerge-datasource (ids_export_ready.xls)
3; Open VB-editor -This document

There you will find 3 simple code lines:
Dim b As Variant
b = ActiveDocument.MailMerge.DataSource.DataFields(3) 'change the index to 3 or 4 to see the error
MsgBox (b)

the second line will fail for index 3 and 4 but work for the others.

Please let me know if you need anything else or have an idea. Any help/hint is highly appreciated

TonyJollans
01-11-2010, 09:17 AM
Please accept my apologies for the long delay in replying. I copied your files and they sat on my desktop unopened till now.

The reason for your problem is the overlong names in your excel column headers:


quoteid_promx_lineitemgroupid_promx_name
quoteid_productid_promx_producttypeid_promx_name

It should all work if you use shorter ones.

advin
01-15-2010, 12:30 AM
Hallo,

thanks for the info. I allready noticed that it works with shorter fieldnames. But I'm quite unsure why it fails?

the problem is, that the datasource is delivered by another tool, so i have no possibilities to shorten the fieldnames in there.

Are there any ways to modify the datasource in VB before accessing the fields?

best regards, advin