Mail Merge questions - Executing the merge

A

Allov

Hi,

We started using the Office Object Library, more specificaly the Word Object
Library with Office 2007 and VB.Net.

We'd like some pointers on some issues we're trying to figure out.

- Using the Mail Merge, is it possible to get an object of the newly created
document using this code? :

Me.Document.Activate()
Me.Document.MailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters
Me.Document.MailMerge.OpenDataSource(odcFile,
Word.WdOpenFormat.wdOpenFormatAuto, False, False, False, False, "", "",
False, "", "", cnnString, sql, "", False,
Word.WdMergeSubType.wdMergeSubTypeOther)
Me.Document.MailMerge.DataSource.FirstRecord =
Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
Me.Document.MailMerge.DataSource.LastRecord =
Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
Me.Document.MailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
Me.Document.MailMerge.Execute(False)

In fact, what's going on right now is that it sends the mail merge to a new
document but we don't really know how to get this new document's object
reference to save it or do something with it.

- Is it possible to create one document per entry?

What we're trying to do is to create a PDF file per entry so we can send it
as an attachment by email. This is something we'd like to link with the first
question since we're already able to save as a PDF file using the SaveAsPDF
patch.

- Is there a way to somehow send a DataSet as a DataSource?

Some background: We checked the process of creating a Mail Merge as an End
User (using word exclusively). However, the only way to create the Mail Merge
template (to get a list of fields) is to plug in a valid DataSource (SQL
Server, Access, Office Address List, etc.)

Is there a way to give a dummy DataSource to the Mail Merge so our customers
don't have directly access to the DB data? Just like a Crystal Report or
Reporting Services report where you just give in some data structure
template. This way, customers won't need us to create formatted mail and will
have to fill in the fields.

However, data could come from the database once executed by code. In fact,
it's preferred.

Thanks a lot!
 
C

Cindy M.

Hi =?Utf-8?B?QWxsb3Y=?=,
We started using the Office Object Library, more specificaly the Word Object
Library with Office 2007 and VB.Net.
Please note that the better resource for questions about Mail Merge and how it
works would be the word.mailmerge.fields newsgroup. That's where you'll find
specialists.

I'll try to give you some "get started" answers, in-line...
We'd like some pointers on some issues we're trying to figure out.

- Using the Mail Merge, is it possible to get an object of the newly created
document using this code? :

Me.Document.Activate()
Me.Document.MailMerge.MainDocumentType =
Word.WdMailMergeMainDocType.wdFormLetters
Me.Document.MailMerge.OpenDataSource(odcFile,
Word.WdOpenFormat.wdOpenFormatAuto, False, False, False, False, "", "",
False, "", "", cnnString, sql, "", False,
Word.WdMergeSubType.wdMergeSubTypeOther)
Me.Document.MailMerge.DataSource.FirstRecord =
Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
Me.Document.MailMerge.DataSource.LastRecord =
Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
Me.Document.MailMerge.Destination =
Word.WdMailMergeDestination.wdSendToNewDocument
Me.Document.MailMerge.Execute(False)

In fact, what's going on right now is that it sends the mail merge to a new
document but we don't really know how to get this new document's object
reference to save it or do something with it.
No, unfortunately this method does not return an object.

1. If you link up the MailMergeAfterMerge event, that does return the result
document as a parameter of the event

2. You can pretty much assume that the active document will be the result
document, but of course it's not guaranteed. A way around that would be to
create a List (collection, array) of all the open documents in the application
object before you Execute() (Word runs synchronously so you can be sure the user
can't do anything once your code starts). Compare the ActiveDocument at the end
of the merge with all the members of the List (collection, array). If it doesn't
match any of them, then it's new and therefore the result.

Or, you can check the Name of the document. A mail merge result always starts
with the same characters, depending on the type of mege.
- Is it possible to create one document per entry?

What we're trying to do is to create a PDF file per entry so we can send it
as an attachment by email. This is something we'd like to link with the first
question since we're already able to save as a PDF file using the SaveAsPDF
patch.
Not with any of the built-in tools, no. You'll find a suggested approach for
breaking the result document down and emailing it (all VBA code) on
word.mvps.org
- Is there a way to somehow send a DataSet as a DataSource?

Some background: We checked the process of creating a Mail Merge as an End
User (using word exclusively). However, the only way to create the Mail Merge
template (to get a list of fields) is to plug in a valid DataSource (SQL
Server, Access, Office Address List, etc.)
No. Word's mail merge technology is a fossil. Beyond adding ODBC, and later OLE
DB, as valid connection methods it hasn't changed since the late 1980's when
Word was first released. Word *requires" a standard, flat-table data source
saved as a file locally, or in a "traditional" network path. No XML, no URLs,
no "streams".
Is there a way to give a dummy DataSource to the Mail Merge so our customers
don't have directly access to the DB data? Just like a Crystal Report or
Reporting Services report where you just give in some data structure
template. This way, customers won't need us to create formatted mail and will
have to fill in the fields.

However, data could come from the database once executed by code. In fact,
it's preferred.
Unfortunately, Word lost this capability a couple of versions back. Used to be,
you could set up a delimited text file with just the data headers (field names),
link that to the document, and the users could insert the field names without
any data needing to be available.

Now, about the best you can do is to provide your own interface for inserting
the merge fields. You provide a list of fields and your code takes care of
inserting them as a Merge Field (Document.Fields.Add method). Then the document
can later be attached to the data source.

As long as the document is a "normal" Word document (which it should be until a
data source is attached) Word won't care whether it contains merge fields.

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