Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Date Fields in Mail Merge with Word
Message
De
03/02/2000 03:49:39
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
02/02/2000 15:27:43
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Divers
Thread ID:
00326356
Message ID:
00326572
Vues:
31
>Using Word 2000, Windows 98, VFP 6.0...
>Have a Mail Merge Document that displays empty date fields as "12/30/1899". Is there a setting or option I can use to correct this? If not, I tried entering {if {mergefield myfieldname} <> " " {mergfield myfieldname}, "UNAPPROVED"}. What I need is, if this date field is empty, print that it is "Unapproved", otherwise print the date. Thanks in advance.


Mary,
I totally dropped using ODBC Datasource for mailmerges. IMHO it has problems started to grow with MDAC 2.1. Next time might be worse (I support fox2x tables with memo too). Also I don't like its internal conversion for numeric data. Instead what I do is :

-Lowlevel open a file and write everything as I want delimited with tabs there
-Read that file to Clipboard
-Create word doc and paste, convert to table
-Set it as Mailmerge datasource doc.
-Activate main doc and bind datasource doc

This way everything goes to word as if all char type. Also I made timing tests on this approach and difference is in milliseconds. Here is a sample :
Wait window nowait "Creating Word Document.Please wait..."
* Word constants header file
#include "wdconst.h"
#Define NL chr(13)
#Define TABULATE chr(9)

* VFP6
lnFields=_CopyDataToClipBoard(_samples+"\data\employee", ;
                "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

oWordDocument=createobject("word.application")	&& Create word object
With oWordDocument
  .documents.add	&& New file
  With .ActiveDocument
    .PageSetup.Orientation = wdOrientLandscape
    .Range.Paste
    .Range.ConvertToTable(wdSeparateByTabs,,lnFields,, ;
                     wdTableFormatContemporary,,,,,,,,,.f.) && Contemporary :)
    .Range.Find.Execute("~",,,,,,,,,"^p",wdReplaceAll) &&Restore memo para marks
  Endwith
  =_SampleMerge() && Previous code demos send table part if this is commented
  .visible = .t.  && Show word app
  .Activate
Endwith
Wait clear
**** Set the LocaleId to the previous value
=sys(3006,val(nlLocaleId))
Return

*******************************************************************
* Insert a table in Word - 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) )
      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
  Lparameters tnField
  lcType = type(field(ix))
  luValue = eval(field(ix))
  Do case
    Case lcType = "D"
      lcValue = dtoc(luValue) && Here you could set "UNAPPROVED"
    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)

Function _SampleMerge
With oWordDocument
  lcTempDataDoc = sys(5)+curdir()+"T"+sys(2015)+".doc"
  With .ActiveDocument
    .SaveAs(lcTempDataDoc)  && Save as a word doc for mailmerge
    .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 if MDAC wasn't here)
    .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...")
Endwith
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform