|
|||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
AccuSoft Support |
|||||||||||||||||||||
|
Using
AccuTerm to Automate Mail Merge Between the MV Database and
Microsoft Word
By
Joe Goldthwaite, Bar-S Foods
In
this article, we're going to explore ways to use Word's ActiveX
Document architecture and AccuTerm's scripting language to
automatically merge Pick data with a Word document.
Microsoft Word is a very powerful word processing system.
With the addition of Active X automation, it gains much
more flexibility. Almost
any feature in Word can be accessed from your Pick based system. A
mail merge operation in Word is a fairly simple process.
You first create a source document and link it to a data
source. From there,
you can insert special codes into the source document that tell
Word where you want the data fields to go.
Word can access a variety of data formats; Excel, Access,
SQL or even text files. The important thing to keep in mind is
that Word needs to be able to match up field names in the source
file with the mail merge fields in the source document.
For our purposes, the perfect format is the tab delimited
text file that reserves the first line for the field names.
This is a simple file format that you can create
automatically with AccuTerm's file transfer utility - FTD.
It is also an easy format to work with.
You can edit and change the file using Notepad, Word
itself, Excel or any number of other Windows programs. Our
sample application consists of three parts.
First the text file mentioned above. The name we're going
to use is "c:\MergeData.txt".
Second, we're going to create a word document that will be
linked to the c:\MergeData.txt file and will act as the source
document. We're going to call it "c:\MergeSample.doc".
Third, we're going to create a script that will be
downloaded from AccuTerm and will instruct Word to do the actual
mail merge. Most
of the work involved in doing a mail merge is handled by our
source document – MergeSample.doc.
If you have some experience with Word mail merges, this
will be much simpler. If not, read on.
Creating a source document is easy.
The only difficulty is remembering the field names.
Word can help us here but only if we first connect the
document to our data before we start inserting the merge fields.
Here's the step by step. 1.
Download a sample data file using FTD so we have a data
source to attach to the new source document. This will allow you
to choose field names to insert into the new source document.
To download the data from Pick, we'll use the FTD command.
We created a Pick file called MERGEDATA.
It has dictionary items for "Name",
"Address", "City", "State",
"Zip". The
names are case sensitive. Here's
the session that created the c:\MergeData.txt file.
The items in bold are the ones we typed.
>FTD AccuTerm
Data Transfer Utility
(S)end,
(R)eceive, (C)onfigure, (O)ptions, (H)elp or (E)xit ? S
File
transfer protocol: (A)SCII or (K)ERMIT ? K
Enter
source (PICK) file name: MERGEDATA
Enter
source (PICK) item list: *
Enter
target (DOS) file name (d:\directory\file.ext): c:\mergedata.txt
Attributes
to transfer: Name Address City State Zip
Generate
Header Record (<Y>/N): Y
Explode
MultiValue Fields (Y/<N>): N
Transfer
status: Successful transfer. Transferred
9 items, 391 bytes.
2.
Start Word and create a new document. 3.
Select Tools->MailMerge from the Word menu
This will display a dialog box with a series of buttons.
The only button that will be active at this point, is the create
button. Select Create
and choose Form Letter from the drop down menu.
You are then given the choice of using the Active Window or
creating a new main document.
We want to use the Active Window. 4.
The Edit and Get Data buttons should now be enabled. The
next step is to connect this document to our data file. Click on
the Get Data button and select Open Data Source.
You should get an open file dialog box.
Go ahead and open the c:\MergeData.txt file. 5. Now we can start actually typing our document. Any place you want to use the data from the MergeData.txt file, click on the Insert Merge Field button and select the desired field name from the drop down list. 6.
After the document looks the way you want it to, save it to
"c:\MergeSample.doc" We
now have a word document that contains our form letter and is
connected to the MergeData.txt file as a data source.
We're now ready for the next step.
Getting our Pick data and performing the mail merge from
AccuTerm's scripting language.
Repeat step 1 with the entire dataset that you want to use
with your form letter. Once
the data source (text file) has been downloaded, all we need to do
is start up Word, load our source document and tell it to do the
mail merge. It
already knows the file with the data source and the field names.
To do this, we'll execute this script:
'First
create our object variables Dim
WordApp As Object Dim
WordDoc As Object Const
SendToPrinter = 1 'Make
sure errors don’t mess us up On
Error Resume Next 'Start
up word Set WordApp = CreateObject("Word.Application") 'Open
our source document Set WordDoc = WordApp.Documents.Open(FileName:="C:\MergeSample.doc", ReadOnly:=True)< /FONT> 'Merge
the document with our data file With
WordDoc.MailMerge .OpenDataSource Name:= "c:\MergeData.txt"
.Destination = SendToPrinter
.SuppressBlankLines = True
.Execute Pause:=False End
With 'That should do it. The only thing left is clean up. This is very important 'If
we don't close both the document and the
application, it will stay 'in
memory. If you try
to run the macro twice, it will
fail because 'the
document is already open in another session.
It also 'uses
up memory WordDoc.Close
savechanges:=False Set
WordDoc = Nothing WordApp.Quit Set
WordApp = Nothing
That’s
it! This simple
script. It opens
Word, and tells it to merge the text file with MergeSample.doc and
send it to the printer. In
order to use this script from your host machine, it’s necessary
to automatically download and run it using AccuTerm’s script
command. This is
simpler than it sounds. AccuTerm
has a command that allows you to download a script.
Script lines are separated by the ASCII EM character
(CHAR(25)). Then we
send a special escape sequence to AccuTerm followed by our script.
AccuTerm will then run it.
To simplify things and to provide an example, this PICK
subroutine handles all of the above.
SUBROUTINE
ATWORDMERGE(DOCUMENT,MERGEDATA) * *
THIS SUBROUTINE EXECUTES A PRESET WORD MERGE WITH A DOCUMENT * * Created 19 March 2001 JEG * Updated 17 March 2005 PJS * * Pass path to template document in DOCUMENT, path to merge data * file in MERGEDATA. * * EQU ESC TO
CHAR(27), STX TO CHAR(2), CR TO
CHAR(13), EM TO CHAR(25) *First
create our object variables SCR
=
'Dim WordApp As Object' SCR
= SCR:EM:'Dim WordDoc As Object' SCR = SCR:EM:'Const SendToPrinter
= 1' *Make
sure errors dont mess us up SCR
= SCR:EM:'On Error Resume Next' *Start
up word SCR = SCR:EM:'Set WordApp
= CreateObject("Word.Application")' *Open
our source document SCR
= SCR:EM:'Set WordDoc = WordApp.Documents.Open(FileName:="':DOCUMENT:'",
ReadOnly:=True)' *Merge
the document with our data file SCR
= SCR:EM:'With WordDoc.MailMerge' SCR = SCR:EM:' .OpenDataSource Name:= "':MERGEDATA:'"' SCR
= SCR:EM:' .Destination
= SendToPrinter' SCR
= SCR:EM:' .SuppressBlankLines
= True' SCR
= SCR:EM:' .Execute
Pause:=False' SCR
= SCR:EM:'End With' *That
should do it. The
only thing left is clean up. This is very important *If
we don't close both the document and the
application, it will stay *in
memory. If you try
to run the macro twice, it will
fail because *the
document is already open in another session.
It also *uses
up memory SCR
= SCR:EM:' WordDoc.Close
savechanges:=False' SCR
= SCR:EM:' Set
WordDoc = Nothing' SCR
= SCR:EM:' WordApp.Quit' SCR
= SCR:EM:' Set
WordApp = Nothing' PRINT
ESC:STX:'P':SCR:CR: RETURN
There you have it. A simple automatic mail merge subroutine that is executed from your Pick host. One enhancement you may want to make to this routine is to perform the FTD transfer of the merge data using EXECUTE and DATA statements. Also, with a little more work it is possible to specify the data file using scripting. An easy way to install this example program is to edit a new item using the ED verb, place the editor in insert mode ("I" command). Then select the sample program from within Word and copy it to the clipboard (Edit -> Copy). Switch to AccuTerm and paste it into the editor (Edit -> Paste). Please direct any comments or questions to Joe Goldthwaite (joe@asent.com). |
|||||||||||||||||||||||
|
Questions? comments? webmaster@asent.com © 2003 AccuSoft Enterprises All rights reserved Please see our disclaimer |
|||||||||||||||||||||||