Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mail merge to Word: Can't set Word datasource
Message
General information
Forum:
Visual FoxPro
Category:
Troubleshooting
Title:
Mail merge to Word: Can't set Word datasource
Miscellaneous
Thread ID:
00154694
Message ID:
00154694
Views:
62
I'm trying to do a simple mail merge from VFP 6 to Word 97. I'm having trouble setting the Word document's datasource programmatically.

I want to:

1) Create the Word document with the appropriate field codes (I or the end user might actually be doing this).

2) Run a VFP program that creates a table (2.x format)

3) Set the Word document's datasource to that table, from VFP.

4) Execute the merge, from VFP.

I'm using code from the KB, which actually isn't even supposed to work, since it ignores the fact that Word can't deal with VFP 3-6 tables, but seems to have the right idea.

I can connect to the datasource just fine if I do it manually through Word, but I don't want the user to have to do this if she replaces the master document.

Can anyone help me with this code? It should be so simple...

* specify data source for the merge
Local lcSource
lcSource = Sys(2003) + "\Thank You.dbf"
* I also tried "c:\ty.dbf"

* create the data source
open database "Member Minder"
select * from Members into cursor tmp
set safety off
copy to (lcSource) type fox2x
set safety on
close data all

* create the ODBC data source, per KB Q181926
DECLARE Integer SQLConfigDataSource in odbccp32.dll Integer, ;
Integer, String, String
settings="DSN=visual foxpro tables"+chr(0)+;
"Description=VFP ODBC Driver"+chr(0)+;
"SourceDB=" + lcSource + chr(0)+;
"SourceType=DBF" + chr(0) + chr(0)
rc = SQLConfigDataSource(0, 1, "Microsoft Visual FoxPro Driver", ;
settings)
* no problem reported here
Assert rc != 0

oWord = CREATEOBJECT("Word.Application")
WITH oWord
* set parameters for OpenDataSource; again, from KB Q181926
dsname = lcSource
wformat = 0
wconfirmconv = 0
wreadonly = 0
wlinktosource = 0
waddtofilelist = 0
wpassworddoc = ""
wpasswordtemp = ""
wrevert = 0
wprotectdoc = ""
wprotecttemp = ""
wconn = ;
"DSN=visual foxpro tables;uid=;pwd=;"+;
"sourcedb=" + lcSource + ";sourcetype=dbf"+;
"exclusive=no;backgroundfetch=yes;collate=machine;"
* this seems to be necessary, according to another KB article
wsqlstatement = "select * from " + lcSource

* open the document that contains the merge fields
Local lcDoc
lcDoc = Sys(2003) + "\Reports\Thank You.doc"
.Documents.Open(m.lcDoc)

* attempt to set the document's merge data source
* error happens here: Unable to open data source
.ActiveDocument.MailMerge.OpenDataSource;
(dsname, wformat, wconfirmconv, wreadonly, wlinktosource,;
waddtofilelist, wpassworddoc, wpasswordtemp, wrevert,;
wprotectdoc, wprotecttemp, wconn, wsqlstatement)

.Documents.Close(0)
.Application.Quit

endwith
Reply
Map
View

Click here to load this message in the networking platform