Wait window nowait "Creating Word Document.Please wait..." * Word constants header file #include "wdconst.h" #Define NL chr(13) #Define TABULATE chr(9) * VFP3-5 *lnFields=_CopyDataToClipBoard(home()+"samples\data\employee","first_name,last_name,title,hire_date,notes") * VFP6 * Copy VFP data to clipboard Select * from (_samples+"\data\employee") where Title like "%Manager%" into table Tempreport lnFields=_CopyDataToClipBoard("Tempreport","first_name,last_name,title,hire_date,notes") *** set the LOCALEID to English nlLocaleId=sys(3004) && Save local id =sys(3006,1033) && We will be sending instructions in English *** set the LOCALEID to English Public oWordDocument oWordDocument=createobject("word.application") && Create word object With oWordDocument .documents.add && New file * .Application.DefaultTableSeparator = "~" With .ActiveDocument .PageSetup.Orientation = wdOrientLandscape && Set to landscape .Range.Paste && Paste VFP data .Range.ConvertToTable(wdSeparateByTabs,,lnFields,,wdTableFormatContemporary,,,,,,,,,.f.) && Convert to table .Range.Find.Execute("~",,,,,,,,,"^p",wdReplaceAll) && Restore memo para marks Endwith =_SampleMerge(.f.) && Do mailmerge .ActiveDocument.MailMerge.Destination = wdSendToNewDocument .ActiveDocument.MailMerge.Execute .ActiveDocument.Range.Find.Execute("^b",,,,,,,,,"^p^p[End of Merge]^p",wdReplaceAll) && Replace section breaks with para marks .visible = .t. && Show word app .Activate && Make it the active foreground app Endwith Wait clear **** Set the LocaleId to the previous value =sys(3006,val(nlLocaleId)) ******************************************************************* * Insert a table in Word at specified bookmark - NonODBC safe way ******************************************************************* Function _CopyDataToClipBoard Lparameters tcTableName, tcFieldList lcOldAlias = Alias() && Save alias Select &tcFieldList ; from (tcTableName) ; nofilter into cursor crsTemp && Select recs into a cursor If reccount("crsTemp")=0 Use in "crsTemp" Return Endif Select crsTemp lnFields = fcount() lcTempFileName = "X"+sys(2015)+".tmp" handle = fcreate(lcTempFileName) && Create a temp file && Write header line For ix = 1 to lnFields =fwrite(handle, field(ix)) If ix < lnFields =fwrite(handle, TABULATE) Endif Endfor =fwrite(handle, NL) Scan && Start scan..endscan For ix = 1 to lnFields =fwrite(handle, TypeConvert(ix) ) && Write converting all to char type If ix < lnFields =fwrite(handle, TABULATE) Endif Endfor =fwrite(handle, NL) Endscan lnSize=fseek(handle,0,2) =fseek(handle,0,0) _Cliptext = fread(handle, lnSize) && Read file to clipboard =fclose(handle) Erase (lcTempFileName) * Prepare text copied to clipboard in VFP which is much faster than Word OLE Use in "crsTemp" If !empty(lcOldAlias) Select (lcOldAlias) Endif Return lnFields Function TypeConvert && Convert VFP data types to char Lparameters tnField lcType = type(field(ix)) luValue = eval(field(ix)) Do case Case lcType = "D" lcValue = dtoc(luValue) Case lcType = "T" lcValue = ttoc(luValue) Case lcType = "N" lcValue = padl(luValue,20," ") Case lcType = "L" lcValue = iif(luValue,"Yes","No") Case lcType $ "MC" && Replace paragraph marks with "~" lcValue = chrtran(strtran(luValue, chr(13)+chr(10), "~"),chr(9),space(4)) Otherwise lcValue = "" Endcase Return alltrim(lcValue) Function _SampleMerge Lparameters tlToPrinter With oWordDocument lcTempDataDoc = sys(5)+curdir()+"T"+sys(2015)+".doc" With .ActiveDocument .SaveAs(lcTempDataDoc) && Save as a word doc for mailmerge - datasource .Close(wdSaveChanges) && Close saving Endwith .documents.add && New file or open a template With .ActiveDocument.MailMerge .OpenDataSource(lcTempDataDoc) && Set saved file as data source for mailmerge (Directly a table could be set via ODBC) .EditMainDocument && Activate the main document Endwith .Application.Selection.TypeText("Dear,"+chr(13)) .ActiveDocument.MailMerge.Fields.Add(.Application.Selection.Range, "LAST_NAME") .Application.Selection.TypeText(", ") .ActiveDocument.MailMerge.Fields.Add(.Application.Selection.Range, "FIRST_NAME") .Application.Selection.TypeText(chr(13)+"Here is your notes data :"+chr(13)+chr(13)) .ActiveDocument.MailMerge.Fields.Add(.Application.Selection.Range, "NOTES") .Application.Selection.TypeText(chr(13)+chr(13)+"As to our records you were hired on ") .ActiveDocument.MailMerge.Fields.Add(.Application.Selection.Range, "HIRE_DATE") .Application.Selection.TypeText(" with title [") .ActiveDocument.MailMerge.Fields.Add(.Application.Selection.Range, "TITLE") .Application.Selection.TypeText("]"+chr(13)+"Thanks"+chr(13)+"Yours Sincerely."+chr(13)+"Blah blah...") EndwithCetin