.OpenDataSource(m.lcSourceDoc) && Set file as data source for mailmerge
>hangs VFP with the infamous Switch to/ Retry error message.>>Select * From customer Where country='USA' Into Cursor mergethese >>MergeIt('mergethese') >> >>Function MergeIt >>Lparameters tcAlias, tcTemplateDoc >>Local nLocaleID,lcTemp,lcHeader >>Local loWord,llToPrinter,lcSourceDoc >> >>*** 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 >> >>* Prepare source >>Select (m.tcAlias) >>lcTemp = Sys(2015)+'.tmp' >>Copy To (lcTemp) Type Delimited With "" With Tab >>AFIELDS(laFields) >>lcHeader = '' >>For ix = 1 To Fcount() >> lcHeader = lcHeader + ; >> Iif(Empty(m.lcHeader),'',Chr(9)) + ; >> Field(m.ix) >>Endfor >>lcSourceDoc = Sys(5)+Curdir()+'MergeSource.txt' >>Strtofile(m.lcHeader+Chr(13)+Chr(10)+Filetostr(m.lcTemp),m.lcSourceDoc) >>Erase (m.lcTemp) >> >>loWord=Createobject("word.application") && Create word object >>With loWord >> If Empty(m.tcTemplateDoc) && No template >> .documents.Add() && New file >> Else >> .documents.Add(m.lcTemplateDoc) && Open a template >> Endif >> With .Activedocument.Mailmerge >> .OpenDataSource(m.lcSourceDoc) && Set file as data source for mailmerge >> .EditMainDocument && Activate the main document >> >>* Just to sample programmatic writing >> .Application.Selection.TypeText("Company : ") >> .Fields.Add(.Application.Selection.Range,'company') >> .Application.Selection.TypeText(Chr(13)+"Dear contact : ") >> .Fields.Add(.Application.Selection.Range,'contact') >> .Application.Selection.TypeText(Chr(13)+"Blah blah ...."+Chr(13)) >>* Just to sample programmatic writing >>*!* FOR EACH oFld IN .Fields >>*!* ? GETWORDNUM(oFld.Code.Text,2) >>*!* endfor >> 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)) >>>>Adding header line separately because having a tab delimited text is better and you can control field names to be different than in source.