=mmerge([select company, contact, title, address, city from (home()+"SAMPLES\DATA\customer") ]) function mmerge lparameters tcSQL, tcTemplate, tnDestination, tlShow, tlWaitWordFinish tlWaitWordFinish = iif(parameters()<5, .T., tlWaitWordFinish) && tlWaitWordFinish default .T. if type("tnDestination") # "N" tnDestination = 0 endif if type("tcTemplate") # "C" ; or !file(tcTemplate) tcTemplate = "" tnDestination = 0 tlShow = .t. endif if tnDestination # 2 tlShow = .t. endif #define LOC_NORECTOREPORT "No records to report !" local lcAlias, lcDefDir,cDSN,cSourceDB,cSourceType,cOther,cSQLStatement,cDataSource, ; lcFreeTable,lcAlias,lcFreeTableName, lnReccount, lnWordWindow lcAlias = alias() && Save current alias if any lcFreeTableName = "F"+right(sys(2015),7) && Create a temp table name * Word doesn't like table names that are not in 8.3 format &tcSQL into table (lcFreeTableName) && Select recs into a temp table lcFreeTable = alias() lcDefDir = sys(5)+curdir() * Prepare DSN strings cDSN = "DSN=FoxPro Files;DBQ="+lcDefDir+";DefaultDir="+lcDefDir+";" cSourceDB = "" cSourceType = "" cOther = "DriverId=536;MaxBufferSize=512;PageTimeout=5;" cDataSource = dbf(lcFreeTable) cSQLStatement = "SELECT * FROM "+lcFreeTableName+".dbf" lnReccount = reccount() cTablePath = dbf() use if lnReccount = 0 messagebox(LOC_NORECTOREPORT) erase (lcFreeTableName+".*") return endif PUBLIC oWordDocument #define autoformat_None 0 #define autoformat_Borders 1 #define autoformat_Shading 2 #define autoformat_Font 4 #define autoformat_Color 8 #define autoformat_AutoFit 16 #define autoformat_HeadingRows 32 #define autoformat_LastRow 64 #define autoformat_FirstColumn 128 #define autoformat_LastColumn 256 #define NoLinkToSource 0 #define LinkToSource 1 #define TableAutoformatType 9 * Colorful 2 - These are enumerated in the order you see \Table\Autoformat menu *** set the LOCALEID to English nlLocaleId=sys(3004) && Save local id =sys(3006,1033) && We will be sending instructions in English WAIT window nowait "Creating Word Document..." && Inform user oWordDocument=createobject("word.basic") && Create word object WITH oWordDocument if !empty(tcTemplate) .filenew(tcTemplate) && open using template table else .filenewdefault && no template - new file endif .InsertDatabase(TableAutoformatType,; autoformat_Borders + autoformat_Shading + autoformat_Font + autoformat_Color + autoformat_Autofit + autoformat_HeadingRows ; ,NoLinkToSource, ; cDSN+cSourceDb+cSourceType+cOther, ; cSQLStatement,,,,cTablePath,,,1) .appshow && Show word app - Word 7.0 support wait clear if !tlShow && No show .fileexit(2) && Exit no save release oWordDocument else .appshow && Show word app DECLARE integer GetForegroundWindow in WIN32API lnWordWindow = GetForegroundWindow() && Save word window handle endif ENDWITH **** Set the LocaleId to the previous value =sys(3006,val(nlLocaleId)) wait clear && Clear wait windows if any if type("oWordDocument") # "U" && oWordDocument still alive if tlWaitWordFinish && Should we wait it to finish DECLARE short IsWindow in WIN32API integer do while IsWindow(lnWordWindow) # 0 && Loop while our word window alive enddo release oWordDocument erase (lcFreeTableName+".*") else return (ltrim(str(lnWordWindow))+"%"+lcFreeTableName) && Return window handle and temp table name. && So caller might deal with it later (handle and tablename is separated with %) endif endif clear dlls if !empty(lcAlias) select (lcAlias) endifIf you won't use memo fields then DataToClip could be the fastest method.
PUBLIC oWordDocument *** set the LOCALEID to English nlLocaleId=sys(3004) && Save local id =sys(3006,1033) && We will be sending instructions in English select company,contact,title ; from (home()+"samples\data\customer") ; into cursor test application.datatoclip("test",reccount(),3) && Copy to clipboard delimited with tabs wait window nowait "Creating Word Document..." oWordDocument=getobject("","word.basic") && Create word object-if already running get it with oWordDocument .appshow && Show word app - Word 7.0 support .filenewdefault && New file, default template .toggleportrait && Toggle page orientation (This is a toggle !) Portrait -> Landscape .editpaste && Paste clipboard data .EditSelectAll && Select entire doc .TextTotable(1) && Convert text to table - tabs (1) (Word table) .editgoto("\Table") && Find the table .nextcell .prevcell && Trick to go to first cell and select - should be an easier way :) for ix= 1 to fcount() && Skip header row-replacing header values with fieldnames .insert(field(ix)) && VFP pasted clipdata padded with spaces - not sutable for merging .nextcell endfor .TableDeleteColumn && Delete extra column endwith wait clear **** Set the LocaleId to the previous value =sys(3006,val(nlLocaleId))Cetin