Possible to implement MailMergeDataSource interface directly?

D

DevDiscuss

I'm researching integrating with Word's mail-merge functionality. I'm
wondering if it is possible to provide the Word object model with a custom
implementation of the MailMergeDataSource interface? Here's what I'm trying
to do from a high level:

1. The user opens Word to create a document template. The user would be
able to insert merge fields using my custom datasource. Ideally, I would
have an Office plugin which would communicate to my application to retrieve
metadata regarding the datasource, etc. Templates would then be persisted
to the database of my application for later use.

2. From within my application, using the Word automation API, I instantiate
Word and load the saved template from step #1. Here I'd set the datasource
to my custom implementation, then execute the Merge.

I would like to avoid recreating the functionality from scratch, and
leverage what is already there in order to have my datasource play nicely
with Word's advanced merge features. I've noticed that the Office Interop
libraries define interfaces for relevant portions of MailMergeDataSource, so
I'm wondering if it is possible to implement these directly or if anyone has
done so?

Thanks
 
C

Cindy M -WordMVP-

Hi DevDiscuss,

No, you can't implemente any of the Office interfaces to "re-use" them. Office
wasn't designed in that manner.

What you describe should work (with a reservation about how you plan to
save/open the templates to your database).

1. No problem using a COM Add-in (load-on-demand?) to link up to your data
source to get a list of field names. And have code-behind-the-list insert the
mergefields for the user.

2. No problem using automation to link Word to the data source and execute the
merge.

If we're not talking Word 2003 XML, then you'll probably have to let the user
save the template to file, then load that into your database. And do the
reverse when the user wants to use the file. The Word object model doesn't
provide for "streaming" documents into/out of the application interface.
I'm researching integrating with Word's mail-merge functionality. I'm
wondering if it is possible to provide the Word object model with a custom
implementation of the MailMergeDataSource interface? Here's what I'm trying
to do from a high level:

1. The user opens Word to create a document template. The user would be
able to insert merge fields using my custom datasource. Ideally, I would
have an Office plugin which would communicate to my application to retrieve
metadata regarding the datasource, etc. Templates would then be persisted
to the database of my application for later use.

2. From within my application, using the Word automation API, I instantiate
Word and load the saved template from step #1. Here I'd set the datasource
to my custom implementation, then execute the Merge.

I would like to avoid recreating the functionality from scratch, and
leverage what is already there in order to have my datasource play nicely
with Word's advanced merge features. I've noticed that the Office Interop
libraries define interfaces for relevant portions of MailMergeDataSource, so
I'm wondering if it is possible to implement these directly or if anyone has
done so?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
D

DevDiscuss

Cindy, thanks for the advice. It sounds like I have a viable approach this
way... I should be able to take my initial list (of contacts for e.g.) and
stream this to a temporary CSV file, which I can then use the Word
automation API to bind the template's mail merge data source against prior
to invoking the merge operation. I would prefer to automate the merging so
the user can accomplish this hands free.

One other source of confusion stems around inserting the merge fields...
perhaps I missed how to automate this via the API. It appeared that the
MailMerge objects field collection is read-only and derived from the
datasource. Is it still possible to allow the user to pick from my custom
field list and insert merge fields *without* linking to a datasource? I'd
like to have my Office add-in provide the selection capability, and insert
the fields automatically. This seems more intuitive vs. having the user
select a datasource manually (in this case a temp file used only for
fetching a field list.)

I hope this all makes sense. Thanks again!
 
C

Cindy M -WordMVP-

Hi DevDiscuss,
One other source of confusion stems around inserting the merge fields...
perhaps I missed how to automate this via the API. It appeared that the
MailMerge objects field collection is read-only and derived from the
datasource. Is it still possible to allow the user to pick from my custom
field list and insert merge fields *without* linking to a datasource? I'd
like to have my Office add-in provide the selection capability, and insert
the fields automatically. This seems more intuitive vs. having the user
select a datasource manually (in this case a temp file used only for
fetching a field list.)
Yes, you can do this, as long as you take care of providing the list and
inserting the fields - IOW you have to provide the entire
field-select-and-insert interface and capability. A Word document doesn't
care if there are MergeFields in the document and no data source attached. It
does care "on open" if there is a data source attached and the merge fields
don't match the field names in the data source.

Note: You'd need to insert the merge fields using the general Fields.Add
method. You can't use anything in the MailMerge hierarchy without a data
source attached.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top