3 |
The LA Fox Developer Newsletter
Seotember 1999
Automating FoxPro, Visual
FoxPro and COM
By:
Mike Merino
Rocky Mountain Fox User Group
The methodology presented here is based on three articles from
FoxPro Advisor issues: June 1996, page 32; July 1999 pages
22 and 43; and December 1996 page 52. Two articles deal with
using VFP 6.0 to execute Word 95 and the other article deals
with sending email from VFP6.0. If you use Word 97 or 2000,
similar methodologies will work but you need to check the Word
Help files and make a few changes to get it to work properly. An
anomaly in using VFP 6.0 and Word95 is that Word 95 has to
be started and minimized prior to executing these examples,
otherwise there will be a VFP 6.0 error. One final note. Although
my examples will use all Microsoft products, these techniques
can be applied to most Win 95/NT applications. Check their
Help files for COM/Automation.
I. EXAMPLE I Filling in a WORD Form Letter from VFP
1. InVFP
a) Make a table with these fields
cName char (15)
cGroup char(10)
dDate Date
cTime Char(5)
cWorktel c(8)
cpagerc(8)
b) Save as c:\tmp\rmfug.dbf in fox2x format
2. Open WORD version 7 and type in a brief letter as follows
Dear fieldi,
Thank you for your interest in our local fieId2. We meet every
field3 at field4
Hope to see you there.
Czar Merino
then select TOOLS/MAIL MERGE/ Form Letter, and use this
letter as the template (e.g. Use Active Window). Next select
Open Data source c:\tmp\rmfug.dbf and then INSERT MERGE
FIELD into spaces field 1-fleld4 using the first four table fields.
Finally save the document as c:\tmp\inv.doc.
3. Start VFP and create form vfp_wordl .scx using data environ-
ment c:\tmp\rmfug.dbf. Make sure to include these fields on the
form, and also add a button to print the form letter. Here is the
sample code for the button:
*
This code is adapted from
*
FoxPro Advisor June 1996 p 32
*
This automation example uses
*
commands from the WORD BASIC
*
language open the database
SELE rmfug
*
create a word
object
WrdObj=createobJect(”word.baslc”) && this statement Is
*
for Word95 only
*
WrdObj=createobject(”word.application”) && this statement is
*
for Word97 only
*
open an empty WORD document window first
WrdObj.FlleNew
*
next open the original document In a second window
WrdObJ.FileOpen(”C:~tmp~inv.doc”)
*
retrieve the number of WORD bookmarks defined In
*
this document
pnCntMarks=WrdObJ.CountBookMarks
Store bookmarks in an array
DIMENSION aMark(pnCntMarks)
FOR pnCount=1 to pnCntMarks
Mark(pnCount)=WrdObj.BookMrkName(pnCount)
ENDFOR
Begin loop through table
*
open a copy of the document in an edit window
WrdObj.FileOpen(”c:~tmp~inv.doc”)
*START (used in example 2)
*
Loop thru all bookmarks
FOR pnCountl to pnCntMarks
pcMrkName=Upper(aMark(pnCount))
a
Move to next bonkmark
WrdObj.EditGoTo(pcMrkName)
DO CASE
CASE pcMrkName= “Name”
WrdObj.lnsert(cname)
CASE pcMrkName= “Group”
WrdObj.insert(cGroup)
CASE pcMrkName= “Date”
WrdObj.Insert(dDate)
CASE pcMrkName= “Time”
IF not empty(cTime)
WrdObj.lnsert (cTlme)
ELSE
WrdObj.lnsert(”Show Up Anytime”)
END1F
ENDCASE
ENDFCR
a
END (used In example 2)
*
Copy modified document to end of master document
WrdObj.StartOfDocument
WrdObj.EndOfDocument(1)
WrdObj.EditCopy
WrdObj.DocClose(2)
WrdObj.EdltPaste
WrdObj.InsertPageBreak
•
Print the Document
WrdObj.FilePrint
WrdObj.FileSaveAs(’C:~tmp\I1 .doc’)
*
Close WORD
WrdObj.DocClose
(Con’t, page 4)
Page 3
|
3 |