Reply
 
Thread Tools Display Modes
  #1   Report Post  
Mark Ainsworth
 
Posts: n/a
Default Splitting text from mail merge fields

I would like to pick up just a specific word, e.g. the first word, from the
contents of a Word mail merge field.
For example, if the mail merge field contains the name in the format
"LastName, FirstName", I want to just display the lastname.

Is there a way of doing this in a mail merge field? If not, is there a way
to call a custom macro from a field so that the formatting of the field can
be performed while the mail merge is executing?

Many thanks,
Mark
  #2   Report Post  
Peter Jamieson
 
Posts: n/a
Default

If possible, you need to do this in the data source. If it's Access, you can
write a query using the instr and left functions to do it. If it's Excel,
you could probably write a function to fill in a new column with the desired
value. For example, if the field containing the data is called myname, the
following SQL expression in an Access query should work:

left(myname,instr(myname+',',',')-1)

You only need the + ',' to deal with cases where myname does not contain a
comma.

If not, is there a way
to call a custom macro from a field so that the formatting of the field
can
be performed while the mail merge is executing?


There isn't a really simple way to do this that just uses Word fields, but
there are several methods that may work, depending on the version of Word
and the data source
a. the "official" method in Word 2002/2003 is probably to use VBA and the
MailMergeBeforeRecordMerge event to do some processing and stuff the result
into the Mail Merge Main document immediately before each record is merged.
This should be feasible regardless of the data source. You get the field
content using Doc.MailMerge.DataSource.Datafields("myname") and use much the
same expression as above to get the correct text.
b. for data sources that Word accesses via SQL dialects with functions such
as instr and left, you can construct a suitable formula in SQL and issue it
in the VBA OpenDataSource call. This should work with data sources
accessible via the Jet engine (Access .mdbs, Excel workbooks, even plain
text files and others) and SQL servers such as SQL Server and Oracle, but
won't work for data sources such as .doc files, .rtf files etc. which Word
accesses through a very simple dialect of SQL. For Jet or ODBC databases the
expression would probably be as above - other databases may have different
names and syntax to do instr and left.
c. you can use a DATABASE field to do the same calculation as in (a) or
(b). This is simpler than you might think but you need to test it to ensure
it works for you. In particular, you may keep getting the "this...will
execute an SQL query" message (I forget the text) which can only be fixed by
patching the registry. The easiest approach is probably to create a
completely empty Access database (i.e. it needs no queries or tables). Let's
say it is called d.mdb and is in c:\d. Then insert the following DATABASE
field by hand - this one works for Word 2002/2003:

{ DATABASE \d "c:\\d\\d.mdb" \c ""
\s "SELECT left('{ MERGEFIELD myname }',instr('{ MERGEFIELD
myname },',',')-1)" }

Notice that
a. all the {} need to be inserted using ctrl-F9
b. if you insert a DATABASE field from the Database toolbar (which I doubt
you will actually be able to do with d.mdb because it has no tables or
queries) there will be a long string inside \c "". You can get rid of it.
c. normally when you insert a database field from the database toolbar,
Word inserts a \h, which causes a heading row to be inserted. By leaving out
the \h and returning only a single value, the result is scalar rather than
tabular, so Word does not create a table, so the result can e anywhere in
the text flow.
d. the previous examples had a variable in the left/instr expression. In
this case the SQL is operating on the literal that Word evaluates from {
MERGEFIELD myname }, so
- you need to surround it using quotes
- you can add a comma directly at the end of the literal instead of using
+','
e. you don't need a FROM clause.

Peter Jamieson

"Mark Ainsworth" wrote in message
...
I would like to pick up just a specific word, e.g. the first word, from the
contents of a Word mail merge field.
For example, if the mail merge field contains the name in the format
"LastName, FirstName", I want to just display the lastname.

Is there a way of doing this in a mail merge field? If not, is there a way
to call a custom macro from a field so that the formatting of the field
can
be performed while the mail merge is executing?

Many thanks,
Mark



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mail merge with attachments AS Mailmerge 5 April 9th 05 09:49 AM
mail merge not showing fields from outlook contacts Drew Mailmerge 2 January 27th 05 06:57 AM
Empty Mail Merge Fields Meggan Microsoft Word Help 1 January 13th 05 06:22 AM
Empty Mail Merge Fields Meggan Microsoft Word Help 0 January 13th 05 01:43 AM
Outline Renee Hendershott Page Layout 2 December 25th 04 03:49 PM


All times are GMT +1. The time now is 09:23 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"