Can a Merge run a Macro or VBA?

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 embed those more advanced text commands 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?

Thanks,
Colin
 
P

Peter Jamieson

The situation is:
a. there is no Word field code designed to execute VBA code (or any other
type of code)
b. in Word 2002 (XP) and later you can use Word's Mailmerge events to
modify the mail merge main document before each source record is merged.
However, this is still not quite the same as the facilities you are used to
in WP
c. in earlier versions of Word, as you suggest, you can consider driving
the entire merge from VBA, e.g. performing a merge for each record in the
source record.
d. as far as doing things like instr etc. are concerned, you can execute
these kinds of functions if you are dealing with a data source that supports
a dialect of SQL that allows you to do it. Broadly speaking, that includes
Access and data sources that can be read using the Access/Jet database
engine, which e.g. includes delimited text files if you happen to be
connecting to them ia ODBC, and "client/server" DBMSs such as SQL Server and
Oracle which also have "full" SQL dialects. However, the only SQL supported
by Word's internal converters which read .doc files is basically SELECT *
FROM WHERE conditions ORDER BY columns. No functions, aliases, multitable
etc.
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)?

A "twist" as far as point (a) is concerned is that it is possible to execute
code using an INCLUDETEXT field and a special text import converter, simply
because an import converter is a dll which can do anything a dll can do.
I've written such a thing but I generally don't recommend using it because
a. it's non-standard (and e.g. you'd have to distribute copies to every
user, and support them)
b. there is a design problem with INCLUDETEXT which makes the approach
quite unwieldy
c. other stuff...
 

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