Re: Creating many letters(mail merge) from a single template in C#
- From: Cindy M -WordMVP- <C.Meister-C@xxxxxxxxxx>
- Date: Tue, 18 Apr 2006 15:37:58 +0200
Hi =?Utf-8?B?TWlrZTk5MDA=?=,
I need to fill up the letters without using any datasource.If you don't have a data source, technically you can't use mail merge.
For example, I already have a list of names, it does not access any db. This
is typical because the user selects a list from the list and says to create
mail.
However, I understand that you and users decide to use the interface to insert
merge fields because it's there and one is familiar with it.
In this case, as a developer, you basically need to loop through all the fields
in the body of the document, extract the datafield name, look up the data and
insert it in place of the field.
Roughly, it would go like this (off the top of my head, so no guarantees on
the exact syntax):
Word.Document doc = WdApp.ActiveDocument
for each (Word.Field fld in doc.Fields)
{
if (fld.Type == wdApp.wdFieldType.wdFieldMergefield)
{
string sfldName = //Extract the field name, here, from the
fld.Code.Text
//Now look up the data
fld.Result.Text = "the data"
}
}
You'll need to look at the form fields the Word version your users have
generates in order to figure out how best to extract the information you need
for the field names.
Note that, because the data insertion could interfere with managing the Fields
collection, you might need to resort to a construct such as this, to loop all
the fields (VBA-speak):
For i = doc.Fields.Count to 0 Step -1
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 :-)
.
- Follow-Ups:
- References:
- Re: Creating many letters(mail merge) from a single template in C#
- From: Cindy M -WordMVP-
- Re: Creating many letters(mail merge) from a single template in C#
- From: Cindy M -WordMVP-
- Re: Creating many letters(mail merge) from a single template in C#
- Prev by Date: RE: Visual C++ 2005 Excel Automation
- Next by Date: Re: Creating many letters(mail merge) from a single template in C#
- Previous by thread: Re: Creating many letters(mail merge) from a single template in C#
- Next by thread: Re: Creating many letters(mail merge) from a single template in C#
- Index(es):
Relevant Pages
|