Optional Merge Fields

P

Pete Bennett

Hi,

I have a letter template (in Word 2000) that can optionally be used to
create a merge master letter (simply by inserting AutoText containing merge
fields). The mail merge operation is being handled entrirely by VBA by the
way.

This works fine, except I would now like to have the same template to have
the capability of handling two data sources (both CSV files). Many of the
fields will be the same, but one CSV file will have more fields than the
other.

Is there any way that I can use truely optional merge fields, as conditional
merge fields can only function if the merge fields are actually there. I
want to avoid (if at all possible) any messages about "this merge field
doesn't exist".

Thanks,
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGV0ZSBCZW5uZXR0?=,

I don't think there's any way to do this as you envision; no way to "blank out"
unused merge fields. Two alternate approaches occur to me, right off hand:

1. Create both letters as AutoText entries. If the one data source is used,
insert that entry; if the other data source is used, the second entry. As each
autotext entry will contain the fields specific to the data source used, the
problem shouldn't occur.

2. Create both data sources with the same fields. Some of the fields will
always be empty (because they'll never be filled with data), but at least
they'll be present in the data source, so IF fields will work with them.
I have a letter template (in Word 2000) that can optionally be used to
create a merge master letter (simply by inserting AutoText containing merge
fields). The mail merge operation is being handled entrirely by VBA by the
way.

This works fine, except I would now like to have the same template to have
the capability of handling two data sources (both CSV files). Many of the
fields will be the same, but one CSV file will have more fields than the
other.

Is there any way that I can use truely optional merge fields, as conditional
merge fields can only function if the merge fields are actually there. I
want to avoid (if at all possible) any messages about "this merge field
doesn't exist".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGV0ZSBCZW5uZXR0?=,
What I can do is reconcile the merge fields in the data source against the
merge fields held in the master document before the merge is executed.

So, this will check the contents of
ActiveDocument.MailMerge.DataSource.FielNames against
ActiveDocument.MailMerge.Fields

And subsequently delete any merge fields fields in the master document that
aren't contained in the data source. I just have to make sure that I don't
mess up the contents of any conditional merge fields and invalidate them.
Luckily for me, the're not a player.

Ironically, the urgency for this has now passed and I won't have to deal
with it for the next four months, but thought you might find my solution of
interest. I've not tested it, but the theory seems sound to me.
Mmmm. In my experience, as soon as you open the document and the data source
doesn't "match", these errors would start displaying. As long as the main
merge document is not attached to a data source when you open it, and you
delete invalid fields before attaching the data source, this could work.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
P

Pete Bennett

Cindy M -WordMVP- said:
Mmmm. In my experience, as soon as you open the document and the data source
doesn't "match", these errors would start displaying. As long as the main
merge document is not attached to a data source when you open it, and you
delete invalid fields before attaching the data source, this could work.

Cindy,

You piqued my interest in this, so I decided to do a little experient and my
theory did seem to hold together..

In my example I have a merge master letter that contains a merge field
(email) that doesn't occur in my data source. Using VBA, when you open the
datasource, no checking is made to reconcile merge fields.. It's on the
'execute' that you get the errors pop up. Most probably things work
differently through Word's User Inteface, but we all know that, don't we? ;-)

Here's my test code..

Sub MergeTest()
Dim aDoc As Document
Dim aField As MailMergeField

Set aDoc = ActiveDocument

'Set the mail merge type
aDoc.MailMerge.MainDocumentType = wdFormLetters

' Connect data source (doesn't validate at this stage)
aDoc.MailMerge.OpenDataSource Name:="C:\Documents and Settings\User\My
Documents\Mailing List.csv"

' Remove surplus merge field from master document
For Each aField In aDoc.MailMerge.Fields
If aField.Type = wdFieldMergeField Then
If InStr(1, aField.Code.Text, "mergefield email", vbTextCompare) > 0
Then
aField.Delete
End If
End If
Next aField

' Do the execute, which won't moan because the extra field has gone!
aDoc.MailMerge.Execute

' Close the master because it's no longer needed
aDoc.Close SaveChanges:=False

End Sub
 

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