Can I evaluate strings during a merge?

C

Colin Higbie

If I want to do advanced text processing during a merge, like extracting a
first initial from the First_Name field or looking for a word in a larger
merge field in order to determine what text appears in a final merge document,
how can I do this?

Possible approaches:
Is there a way to include a Field code that automatically runs some VBA code I
wrote, so that it is executed and evaluates each record as it is merged,
looking at the first character or searching for a word within the text (like
Instr in VBA)?

Is there a way to just embed those more advanced text commands directly into
the Merge main document (the form)?


I'm coming at this from WordPerfect where such things were trivial - you could
embed any command into the Form document and it would be executed during the
merge. The equivalent in Word would be being able to enter VBA commands as
Field codes so they are carried out during the merge. As far as I can tell,
this is impossible. The next best thing, I think, would be to be able to
launch VBA code and pass it parameters during the merge. I cannot figure out
how to do this.

Maybe I'm looking at the problem all wrong, because I am still thinking like a
WordPerfect user. Is there a whole other approach I need to take? Maybe I need
to think of the VBA running the merge, instead of the merge calling my VBA
code? If so, how would I do that and how would I specify where to do the
string processing I need?

If you need more information from me to be able to answer my questions, please
let me know.

Thanks,
Colin
 
W

Word Heretic

G'day "Colin Higbie" <CHigbie@nospamxxxAptagen.com>,

hmmm, if fields... hmmm

Colin Higbie said:
If I want to do advanced text processing during a merge, like extracting a
first initial from the First_Name field or looking for a word in a larger
merge field in order to determine what text appears in a final merge document,
how can I do this?

Possible approaches:
Is there a way to include a Field code that automatically runs some VBA code I
wrote, so that it is executed and evaluates each record as it is merged,
looking at the first character or searching for a word within the text (like
Instr in VBA)?

Is there a way to just embed those more advanced text commands directly into
the Merge main document (the form)?


I'm coming at this from WordPerfect where such things were trivial - you could
embed any command into the Form document and it would be executed during the
merge. The equivalent in Word would be being able to enter VBA commands as
Field codes so they are carried out during the merge. As far as I can tell,
this is impossible. The next best thing, I think, would be to be able to
launch VBA code and pass it parameters during the merge. I cannot figure out
how to do this.

Maybe I'm looking at the problem all wrong, because I am still thinking like a
WordPerfect user. Is there a whole other approach I need to take? Maybe I need
to think of the VBA running the merge, instead of the merge calling my VBA
code? If so, how would I do that and how would I specify where to do the
string processing I need?

If you need more information from me to be able to answer my questions, please
let me know.

Thanks,
Colin

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
Email (e-mail address removed)
Products http://www.geocities.com/word_heretic/products.html

Replies offlist may require payment.
 
C

Cindy Meister -WordMVP-

Hi Colin,

Formulated very simply: Word fields don't support string manipulation. So what
you want to do is definitely not straight-forward in Word.

What's your data source? And which version of Word are we talking about?

Note that if you'd posted this question in the word.mailmerge.fields group,
you'd have probably gotten a useful answer a long time ago...
If I want to do advanced text processing during a merge, like extracting a
first initial from the First_Name field or looking for a word in a larger
merge field in order to determine what text appears in a final merge document,
how can I do this?

Possible approaches:
Is there a way to include a Field code that automatically runs some VBA code I
wrote, so that it is executed and evaluates each record as it is merged,
looking at the first character or searching for a word within the text (like
Instr in VBA)?

Is there a way to just embed those more advanced text commands directly into
the Merge main document (the form)?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

Colin Higbie

Cindy,

Thanks for your response. In fact I had posted a very similar question there,
and was also pretty much told that it couldn't be done. I was hoping there was
a way to do it using VBA, since apparently it can't be done as part of Field
Codes during a merge.

This seems like a fairly fundamental feature, no? In WordPerfect, you can
embed code into the document, which is executed during the merge. The
equivalent in Word would be if you could embed VBA codes, not just the limited
Field Codes, into the form to be executed during the merge (or at least having
a much expanded set of Field Codes).

I am using Word 2002 (XP). My form and data files were both created by Word -
I am not using a separate address book or database. The data needs to conform
to a certain format because the same data file has to work with several forms.
Therefore, I can't just change the data to be in the perfect form - I need the
forms to be able to
figure out what the data is and act appropriately. If I absolutely had to, I
could work with the data files in MS Access, but I don't have that on all the
computers I need to work on and I'd prefer a Word/VBA solution.

Here are some other examples of what I want to do (all features that I had in
the WordPerfect docs I want to migrate):

1. Create a document name by extracting the first initial from the First Name
field (by combining it with the date and last name as in JSmith.09.20.03.01).
2. Perform calculations based on a date stored in the data to determine
mandatory dates for upcoming events, specifically a vesting schedule for stock
options. So if the Effective Date holds September 20, 2003, and a Vesting
Period field holds 4 (stored in months), it can calculate upcoming vesting
dates (January 20, 2004; May 20, 2004; etc.) [I think I can do this with Field
Codes - haven't tried yet, but I see there are date and mathematical functions
included.]
3. Other string ops, like looking at names to see if they include certain
modifiers like Dr. and changing the methods of greeting (changing how formal
parts of a letter are based on the name). Similarly, if the name contains
"and" that means it's probably a man and wife and therefore needs a different
greeting than for an individual.

Does this help? I understand that I can't do these things as part of a
Form/Data merge, but with the power of VBA, surely I can develop some way to
accomplish these tasks. Maybe something that opens both the Form and Data and
control what happens. I know that Word and WordPerfect behave differently, so
I won't hold it against Word if I have to go through a couple of extra steps,
but please don't tell me that Word can't perform string manipulation at all
when merging data.

Thanks again and in advance for any additional assistance you can provide,
Colin
 
C

Cindy Meister -WordMVP-

Hi Colin,
I am using Word 2002 (XP).
OK, this gives you some more leeway than any earlier
version would. Word 2002 introduced mail merge EVENTS -
most specifically MailMergeBeforeRecordMerge - that allow
you to manipulate that data "as you go".

To get an idea about this, search my name on the msdn
website. Look for the article on what's new in Word 2002.
If I remember correctly, I put some sample code in there to
demonstrate how the events work. Take a look at that, then
come back with any questions you can't resolve. (Probably
easier to catch me in the mailmerge group, BTW)

Note: 1 and 2 could be accomplished without this. 1 would
still require VBA, but could be performed on the merge
result. For 2, you could use the tool at
http://www.chriswoodman.co.uk/Delayed Dates.htm. Things
like (3) could also be done in the merge result, but it
would be very complex.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
J

JGM

Hi Colin,

Another avenue to explore:

For your point #3:

In the document template, Bookmark the Name part, then in the greeting part,
use IF fields to check the content of the name part and display text as
appropriate. You can use nested IF fields. Look up the help topic on IF
fields, it gives examples of simple and nested IF fields.

HTH
Cheers.
--
_________________________________________

Jean-Guy Marcil
(e-mail address removed)

Colin Higbie said:
Cindy,

Thanks for your response. In fact I had posted a very similar question there,
and was also pretty much told that it couldn't be done. I was hoping there was
a way to do it using VBA, since apparently it can't be done as part of Field
Codes during a merge.

This seems like a fairly fundamental feature, no? In WordPerfect, you can
embed code into the document, which is executed during the merge. The
equivalent in Word would be if you could embed VBA codes, not just the limited
Field Codes, into the form to be executed during the merge (or at least having
a much expanded set of Field Codes).

I am using Word 2002 (XP). My form and data files were both created by Word -
I am not using a separate address book or database. The data needs to conform
to a certain format because the same data file has to work with several forms.
Therefore, I can't just change the data to be in the perfect form - I need the
forms to be able to
figure out what the data is and act appropriately. If I absolutely had to, I
could work with the data files in MS Access, but I don't have that on all the
computers I need to work on and I'd prefer a Word/VBA solution.

Here are some other examples of what I want to do (all features that I had in
the WordPerfect docs I want to migrate):

1. Create a document name by extracting the first initial from the First Name
field (by combining it with the date and last name as in JSmith.09.20.03.01).
2. Perform calculations based on a date stored in the data to determine
mandatory dates for upcoming events, specifically a vesting schedule for stock
options. So if the Effective Date holds September 20, 2003, and a Vesting
Period field holds 4 (stored in months), it can calculate upcoming vesting
dates (January 20, 2004; May 20, 2004; etc.) [I think I can do this with Field
Codes - haven't tried yet, but I see there are date and mathematical functions
included.]
3. Other string ops, like looking at names to see if they include certain
modifiers like Dr. and changing the methods of greeting (changing how formal
parts of a letter are based on the name). Similarly, if the name contains
"and" that means it's probably a man and wife and therefore needs a different
greeting than for an individual.

Does this help? I understand that I can't do these things as part of a
Form/Data merge, but with the power of VBA, surely I can develop some way to
accomplish these tasks. Maybe something that opens both the Form and Data and
control what happens. I know that Word and WordPerfect behave differently, so
I won't hold it against Word if I have to go through a couple of extra steps,
but please don't tell me that Word can't perform string manipulation at all
when merging data.

Thanks again and in advance for any additional assistance you can provide,
Colin

Cindy Meister -WordMVP- said:
Hi Colin,

Formulated very simply: Word fields don't support string manipulation.
So
what
you want to do is definitely not straight-forward in Word.

What's your data source? And which version of Word are we talking about?

Note that if you'd posted this question in the word.mailmerge.fields group,
you'd have probably gotten a useful answer a long time ago...
VBA
code I directly
into

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question
or
reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top