As of the date of this document, there are no ODBC drivers for Level 7 dBASE tables. So how do you use a dBASE table as a data source for Word? Simple, you create a temporary Level 4 table and use that. Copy4.zip is a CC included with this zip file to accomplish this task. Copy4.cc contains its own documentation in the form of header notes. For this document, we'll just use it and leave the explanations for the cc file.
// Specify a file and path for our temp table
#define dsName "C:\My Documents\Temp.dbf"
#define dsPathOnly left( dsName, rat( '\', dsName ) - 1)
// The body of our "letter"
cBody = "Acme Widgets is happy to announce a special promotion, available " + ;
"only within the state of California. For a limited time only, our " + ;
"California clients may purchase the new SuperWidget, with California " + ;
"emission controls, for only $42,386,941.22. We hope you will take " + ;
"advantage of this special sale right away." + chr(13) + chr(13) + ;
"Thank you for taking the time to read this. We, at Acme Widgets, " + ;
"look forward to hearing from you in the near future."
try
set procedure to Copy4.cc additive
if file( dsName )
erase ( dsName )
endif
// begin by trying to get our data for the merge source
d = new database()
d.databaseName := 'MUGS'
d.active := true
q = new query()
q.database := d
q.sql := 'select customer."First Name",customer."Last Name", ' + ;
'Street1, Street2, City, customer."State ID", Postal from customer ' + ;
'where customer."State Id"="CA"'
q.active := true
set procedure to Copy4.cc additive
Copy4( q.rowset, dsName )
q.active := false
d.active := false
release object q
release object d
close procedure Copy4.cc
t = new tableDef()
t.tableName = dsName
t.load()
cText = '' // place our field names for column headers
for i = 1 to t.fields.size
cText += t.fields[i].fieldName
if i < t.fields.size
cText += chr(9)
endif
endfor
release object t
oWord = new oleautoclient('word.application')
oWord.visible = true
// create a new document
oDoc=oWord.documents.add()
// save a reference to this doc so we can come back to it later
n=oWord.activedocument.name
// make it a form letter
oWord.activeDocument.MailMerge.MainDocumentType = 0
// get a reference to the MailMerge object
oMerge=oDoc.mailmerge
// next, we OPEN the datasource
oMerge.OpenDataSource( dsName, ; // name
0, ; // Format - wdOpenFormatAuto
false, ; // ConfirmConversions
true, ; // ReadOnly
true, ; // LinkToSource
false, ; // AddToRecentFiles
"", ; // PasswordDocument
"", ; // PasswordTemplate
false, ; // Revert
"", ; // WritePasswordDocument
"", ; // WritePasswordTemplate
"DSN=dBASE Files;DBQ=" + dsPathOnly + ";", ;
"select * from temp.dbf" )
// connection - NOTE the use of semi-colons
// now activate the mail merge document
oWord.documents(n).activate()
// get a little shorter reference to save some typing
oDoc=oWord.activeDocument
// center the date at the top of the page
oWord.selection.ParagraphFormat.Alignment := 1
oWord.selection.TypeText("Acme Widgets Corporation")
oWord.selection.TypeParagraph()
oWord.selection.TypeText("1234 Main Street")
oWord.selection.TypeParagraph()
oWord.selection.TypeText("MyTown, AA 12345")
oWord.selection.TypeParagraph()
oWord.selection.InsertDateTime("MMMM d, yyyy",true)
oWord.selection.TypeParagraph()
oWord.selection.ParagraphFormat.Alignment := 0
for i = 1 to 3
oWord.selection.TypeParagraph()
endfor
oDoc.mailMerge.Fields.add(oWord.selection.range, "First_Name")
oWord.selection.TypeText(" ") // space between first and last name
oDoc.mailMerge.Fields.add(oWord.selection.range, "Last_Name")
oWord.selection.TypeParagraph() // new line
oDoc.mailMerge.Fields.add(oWord.selection.range, "Street1")
oWord.selection.TypeParagraph() // new line
oDoc.mailMerge.Fields.add(oWord.selection.range, "Street2")
oWord.selection.TypeParagraph() // new line
oDoc.mailMerge.Fields.add(oWord.selection.range, "City")
oWord.selection.TypeText(", ")
oDoc.mailMerge.Fields.add(oWord.selection.range, "State_ID")
oWord.selection.TypeText(" ")
oDoc.mailMerge.Fields.add(oWord.selection.range, "Postal")
oWord.selection.TypeParagraph() // new line
oWord.selection.TypeParagraph() // new line
oWord.selection.TypeText("Dear ")
oDoc.mailMerge.Fields.add(oWord.selection.range, "First_Name")
oWord.selection.TypeText(",")
oWord.selection.TypeParagraph() // new line
oWord.selection.TypeParagraph() // new line
oWord.selection.TypeText( cBody )
oWord.selection.TypeParagraph()
oWord.selection.TypeParagraph()
oWord.selection.TypeText("Sincerely,")
oWord.selection.TypeParagraph()
oWord.selection.TypeParagraph()
oWord.selection.TypeParagraph()
oWord.selection.TypeParagraph()
oWord.selection.TypeText("John Smith")
// now, just for grins - make the whole thing
// Times New Roman font 12pt
with ( oWord.ActiveDocument.Styles("Normal").Font )
Name = "Times New Roman"
Size = 12
Bold = False
Italic = False
endwith
With ( oWord.ActiveDocument.Styles("Heading 2").Font )
Name = "Times New Roman"
Size = 18
Bold = True
Italic = True
endwith
With ( oWord.ActiveDocument.Styles("Heading 2").ParagraphFormat )
Alignment = 1 // center
endwith
oWord.Selection.GoTo( 3, 1 )
oWord.Selection.style = oWord.ActiveDocument.Styles("Heading 2")
// now execute the mail-merge
oDoc.mailMerge.destination := 0
oDoc.mailMerge.execute(false)
// we can now close the mail merge document leaving
// only the merged results document
oDoc.close( 0 )
oWord.visible = true
catch (exception e)
clear
? e.code
? e.message
? e.lineNo
if type('oWord') == 'O'
try
oWord.quit(0)
catch ( exception e )
endtry
endif
endtry
[MS Word Mail-Merge.how] [Data Source Word Doc] [Data Source Excel]
Last modified: November 9, 1999