Re: Creating many letters(mail merge) from a single template in C#
- From: Mike9900 <Mike9900@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 19 Apr 2006 00:38:01 -0700
Thanks for the help.
But I want to produce many of this letter, for example I have many
recipients. I can use for loop, but it does not work. My code is something
like this:
//Here fo 10 recipients generate 10 letters
for (int i = 0; i < 10; i++)
{
String company = "company " + i.ToString();
String address ="Address " + i.ToString();
foreach (Word.Field fld in doc.Fields)
{
if (fld.Type == Word.WdFieldType.wdFieldMergeField)
{
string sfldName = "";
if (fld.Result.Text == "«CompanyName»")
{
fld.Result.Text =company;
}
else if (fld.Result.Text ==
"«CompanyBusinessAddress»")
{
fld.Result.Text = address;
}
}
}
}
But this code does not produce 10 letters, only override the fields.
--
Mike
"Cindy M -WordMVP-" wrote:
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:
- 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#
- 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#
- From: Cindy M -WordMVP-
- Re: Creating many letters(mail merge) from a single template in C#
- Prev by Date: Re: Creating many letters(mail merge) from a single template in C#
- Next by Date: Re: Office SDK samples fail after Office SP2 installed
- 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
|
Loading