>>#include "wdconst.h" >>Select * ; >> from (_samples+'data\customer') ; >> where country = 'USA' ; >> into Cursor Tempreport >> >>lcTemp = Sys(2015)+'.tmp' >>lnFields = Fcount() >>Copy To (lcTemp) Type Delimited With "" With Tab >>lcHeader = '' >>For ix = 1 To Fcount() >> lcHeader = lcHeader + Iif(Empty(m.lcHeader),'',Chr(9)) + ; >> 'A_long_header_for_field_'+Field(m.ix)+'_here' >>Endfor >> >>lcSourceDoc = 'c:\temp\Source.txt' >>Strtofile(m.lcHeader+Chr(13)+Chr(10)+; >> Filetostr(m.lcTemp),m.lcSourceDoc) >>Erase (m.lcTemp) >> >>*** 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 >> >>Local oWordDocument, llToPrinter, lcTempDataDoc >>oWord=Createobject("word.application") && Create word object >>With oWord >> .documents.Add && New file or open a template >> With .Activedocument.Mailmerge >> * >> * Set file as data source for mailmerge >> * File is a tab delimited text file with header line >> * Since word's default settings perfectly fit >> * opening a tab delimited file as if it were a table >> * this is just we want - no need for a conversion >> * >> * Or thinking any Word owner should also have Excel >> * (not true always) you might open the text file with >> * Excel and set that as a datasource >> * >> .OpenDataSource(m.lcSourceDoc) >> .EditMainDocument && Activate the main document >> Endwith >> .Application.Selection.TypeText("Dear,"+Chr(13)) >> With .Application.Selection.Font >> .Name = 'Times New Roman' >> .Italic = .T. >> .Size = 14 >> Endwith >> .Activedocument.Mailmerge.Fields.Add(.Application.Selection.Range, ; >> 'A_long_header_for_field_Contact_here') >> .Application.Selection.TypeText(Chr(13)) >> .Activedocument.Mailmerge.Fields.Add(.Application.Selection.Range, ; >> 'A_long_header_for_field_Company_here') >> With .Application.Selection.Font >> .Italic = .F. >> .Name = 'Arial' >> .Size = 10 >> Endwith >> .Visible = .T. && Show word app >> .Activate && Make it the active foreground app >>Endwith >> >>**** Set the LocaleId to the previous value >>=Sys(3006,Val(nlLocaleId)) >>>>As you see this is a sample to OpenDataSource BUT using an office friendly source which is simply another word doc (and word already knows how to open a txt file, simple and fast).
>>With oWord >> .documents.Add && New file or open a template >>>>If you instead write :
>>With oWord >> .documents.Add("c:\myPath\MyTemplate.doc") >>You'd be using a presaved template.