Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word Mailmerge Woes
Message
From
09/12/2006 06:34:16
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
08/12/2006 21:32:53
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows 2000 SP4
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01175633
Message ID:
01176357
Views:
14
I haven't tried XML and Word. New versions should be able to do that but we are already trying to avoid to be version dependant:)
For word another word document with a table in it is a perfect fit for mailmerge source, or an excel sheet (which you can use for OpenDataSource). If you use excel, ADO is the easiest reliable way to transfer data.
In word2003 you can use ADO instead of OpenDataSource (I don't know how yet in code) specifying a udl in source.
Anyway here is a sample using a word doc as a source:
#include "wdconst.h"
#Define NL Chr(13)
#Define TABULATE Chr(9)
Select first_name,last_name,notes ;
  from (_samples+'data\employee') ;
  where Title = 'Sales' ;
  into Cursor Tempreport
lnFields = _CopyDataToClipBoard('TempReport')
Use In 'TempReport'

lcSourceDoc = Sys(5)+Curdir()+'Source.doc'

*** set the LOCALEID to English
nlLocaleId=Sys(3004)  && Save local id
=Sys(3006,1033)       && sending instructions in English
*** set the LOCALEID to English

Local oWordDocument
oWord=Createobject("word.application")	&& Create word object
With oWord
  .documents.Add  && New file or open a template
  With .ActiveDocument
    .Range.Paste
    .Range.ConvertToTable(wdSeparateByTabs)
    .Range.Find.Execute("~",,,,,,,,,Chr(13),wdReplaceAll) && Restore memo para marks
    .SaveAs(lcSourceDoc)
    .Close(wdSaveChanges)   && Close saving
  EndWith
  _cliptext = ""
  
  .documents.Add
  With .ActiveDocument.Mailmerge
    .OpenDataSource(m.lcSourceDoc) && Set file as data source for mailmerge
    .EditMainDocument	&& Activate the main document

    * Write mailmerge text thinking not using a template
    .Application.Selection.TypeText("Dear,"+Chr(13))
    With .Application.Selection.Font
      .Name = 'Times New Roman'
      .Italic = .T.
      .Size = 14
    Endwith
    .Fields.Add(.Application.Selection.Range, 'First_Name')
    .Application.Selection.TypeText(',')
    .Fields.Add(.Application.Selection.Range, 'Last_Name')
    With .Application.Selection.Font
      .Name = 'Times New Roman'
      .Italic = .F.
      .Size = 10
    Endwith
    .Application.Selection.TypeText(NL+NL+'Please update your info and send back. Current notes are as follows:'+NL+NL)
    With .Application.Selection.Font
      .Name = 'Arial'
      .Size = 12
      .Color = Rgb(0,0,255)
    Endwith
    .Fields.Add(.Application.Selection.Range, 'Notes')

    * Send to a new doc - to check what we get
    .Destination = wdSendToNewDocument
    .Execute

  Endwith
  .Visible = .T.  && Show word app
  .Activate  && Make it the active foreground app
*!*	  m.lcPageList = "3,6-7"
*!*	  .ActiveDocument.Printout(.t.,,wdPrintRangeOfPages,,,,,1,m.lcPageList)
Endwith

**** Set the LocaleId to the previous value
=Sys(3006,Val(nlLocaleId))

*******************************************************************
* Insert a table in Word - NonODBC safe way
*******************************************************************
Function _CopyDataToClipBoard
  Lparameters tcCursorName

  lcOldAlias = Alias()  && Save alias
  Select (m.tcCursorName)
  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) )
      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
  If !Empty(lcOldAlias)
    Select (lcOldAlias)
  Endif
  Return lnFields

Function TypeConvert
  Lparameters tnField
  lcType = Type(Field(ix))
  If lcType = 'G'
    Return ''
  Endif
  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 $ "M" && Replace paragraph marks with "~"
      lcValue = Strtran(luValue, Chr(13)+Chr(10), "~")
    Case lcType $ "C"
      lcValue = luValue
    Otherwise
      lcValue = ""
  Endcase
  Return Alltrim(lcValue)
Cetin


>As I said in a previous note, I've changed to text files and it works very well. On thinking more about this though, it occurs to me that memo fields don't export to text files. I thought about using XML, but I was surprised to find that Word won't accept XML for mail merge. Or did I miss something? Do you know if there is a way to use XML files in a word mailmerge?
>
>>>This is driving me crazy. In trying to mailmerge in MS Word using a VFP free table, the code uses the Word VBA method "oWord.ActiveDocument.MailMerge.OpenDataSource()". That method takes up to 15 parameters, only the first of which is not optional. The 13th parameter is an SQL statement - in this case a simple 'select * from ...'.
>>>
>>>I've run into a really oddball situation. My colleague and I are both running Win2K SP4 with Word 2002 SP3 (our office standard right now). We are both able to manually mailmerge in Word using the VFP table, so the ODBC appears to be properly installed in both our cases.
>>>
>>>BUT... if I include the 13th parameter in my code (the Sql statement) Word refuses to recognise that the table contains any fields when run on his system. In other words it fails. On my system, it runs like a champ.
>>>
>>>If I remove that 13th parameter, then on my system it fails and on his system it works beautifully.
>>>
>>>We've run into the same sort of situation with some of our clients, and I feel like I'm beating my head against the wall. I have no idea what is different about his and my systems that might cause such strange behaviour.
>>>
>>>I can't find any sort of setting in Word that looks like something that might affect this, and our odbc setups appear to be exactly the same.
>>>
>>>The only real difference is that I have full VFP on my system and he has only the runtimes (same version though (9 SP1). Of course, our clients all only have the runtimes, so I don't think that's going to have anything to do with it.
>>>
>>>Does anyone have any suggestions about where I might look, or what might be causing this?
>>
>>Alan,
>>IMHO do not use OpenDataSource() with VFP. Simply use a txt file instead. You can search on details 'why not' and sample code here.
>>Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform