View Single Post
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Cindy M -WordMVP- Cindy M  -WordMVP- is offline
external usenet poster
 
Posts: 370
Default Header / Footer mail merge issue with custom built word automation solution.

Hi Jimmi,

Hmmm. "Mail merge" is a very specific thing in Word terminology. Given that
you're working with a web service, you can't be doing a true mail merge, so you
shouldn't use that term when discussing your project - it confuses people like
us :-)

OK, so you're passing data into "targets" in a Word document, and you've chosen
to use Mergefields (I guess, it's hard to be sure if that's what you really
have) as the targets. This works in the body of the document, but not in the
header/footer.

The reason is fairly clear: you're trying to use the Selection object. Fact is,
physically selecting a header/footer via automation in a Word document just
doesn't work very well. Actually, you should avoid *selecting* anything when
automating Word, unless there's no other way to accomplish the task.

It's much better to work exclusively (if possible) with the RANGE object. You
do this up the point of passing control to the ProcessFields method. A field
obejct doesn't have a Range property, but it does have a Result property which
returns a Range. You do not have to select the field in order to access the
Result property.

Next problem: Word fields are actually dynamic entities - they aren't meant to
serve as "data targets". With one single exception (form fields), if you assign
something to Field.Result.Text that information will be lost as soon as the
field receives a command to update (such as pressing F9 when the field is
selected). This is how Word is designed to work.

If the data you're passing in should be permanent, then you should work more
like this:
Word.Field fld = footer.Fields[i];
Word.Range rng = fld.Result;
fld.Delete;
rng.Text = "my data";
[i]
Office professional 2003
Visual studio 2005
C#
Example mail merge field from header: «Field:Name»

I have smart client that use web services to provide data for mail
merge. The templates contain Custom tokens inserted in the document as
mail merge fields eg {{Field:StudentNumber}}, {{Table:CourseUnits}}.
The client parses the tokens and populates document with the relevant
data. All works well until these fields are in the header or footer.

The main method for performing the merge loops through the supplied
data rows and calls the ProcessFields method passing in the fields
collection from the document, header and footer.


Code stub from the execute method
DataRowCollection rows = Data.Tables[0].Rows;
for (int i=0; i rows.Count; i++)
{
Word.Document document = null;
if ( DocumentType == DocumentType.Template)
{
document = Assistant.AddDocument( FilePath, true);
}
else
{
document = Assistant.OpenDocument( FilePath, true);
}
if ( BeforeRowProcessed != null)
{
BeforeRowProcessed(this, document, rows[i], i);
}
// document.MailMerge.Destination = Destination;
Word.HeaderFooter header =
document.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
if (header.Exists)
{
ProcessFields(header.Range.Fields, rows[i]);
}
ProcessFields(document.Fields, rows[i]);
Word.HeaderFooter footer =
document.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
if (footer.Exists)
{
ProcessFields(footer.Range.Fields, rows[i]);
}
if ( AfterRowProcessed != null)
{
AfterRowProcessed(this, document, rows, i);
}
}


private void ProcessFields(Word.Fields fields, DataRow row)
{
foreach (Word.Field field in fields)
{
field.Select();
Word.Range range = Assistant.Application.Selection.Range;
if ( OnFieldSelect != null)
{
OnFieldSelect(this, field, row);
}
ResolveToken(field, row);
}
}


The resolve ResolveToken method maps the word merge field to the
database field writing the text with:

field.Result.Text = DatabaseAssistant.SafeStringValue(row[tokenKey]);

The best I have mamanged is to have either of the header or footer edit
pane open with the resolved value however when I close the window the
merge field only is displayed - the value is gone. I cannot see the
value in the print preview either. Initially I'm only concerned with
the primary header or footer - old tackle the variations of headers /
footers at a later stage.

This is the first Word automation I've built so any pointers will be
appreciated.


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