Re: Creating many letters(mail merge) from a single template in C#



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.
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.

If you don't have a data source, technically you can't use mail merge.

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 :-)


.



Relevant Pages

  • Re: Creating many letters(mail merge) from a single template in C#
    ... For each repeat of the loop you must use the ADD method on the *.dot file to ... //Here fo 10 recipients generate 10 letters ... But this code does not produce 10 letters, ...
    (microsoft.public.office.developer.automation)
  • Re: printing envelopes from a letter merge
    ... Just use the same datasource that you used to create the letters. ... Please respond to the Newsgroup for the benefit of others who may be ... > without going out and re-creating the recipients? ...
    (microsoft.public.word.mailmerge.fields)
  • Re: [QUIZ] Word Loop (#149) [SOLUTION]
    ... letters = word.split ... puts "No loop" ... puts letters ... Support Ruby Quiz by submitting ideas as often as you can: ...
    (comp.lang.ruby)
  • Re: Variable attachment and recipient vb
    ... Simply loop thorugh your recipients first, read their names, addresses or ... Within that loop start ... > Dim objoutlook As Object ... > Set objoutlookmsg = Nothing ...
    (microsoft.public.outlook.program_vba)
  • Re: DO D1%I=1,10
    ... the one about consisting of letters, ... digits, and underscores, with the first character being a letter. ... variable, copying it into the structure as needed, perhaps as the first ... line of the DO loop (and maybe after the loop if you also need to catch ...
    (comp.lang.fortran)

Loading