Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Passing data to microsoft word
Message
From
20/05/2010 06:44:48
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
19/05/2010 20:19:59
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows XP
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01465195
Message ID:
01465242
Views:
81
>I need to print contracts with Word, but want to create and sens the data from foxpro, so that the user does not have to open word, the contract will be printed automatically.

Then you may use word mailmerge. You can either type all of it from within VFP using automation (not a wise thing to do everything in code) or simply use a template. Here is a sample (typing the text myself but just for sampling normally would add a new document using a template file):
*!*	Author: Cetin Basoz
*!*	MailMerge sample that works with newer versions too

*!*	Update: That is what I thought. But Office team likes to 
*!*	change things with every new version. Doesn't work under
*!*	Office 2010 64 bits as is and I don't know why yet.

Wait Window Nowait "Creating Word Document.Please wait..."

#Define wdOpenFormatAuto	0
#Define wdSendToNewDocument	0
#Define wdSendToPrinter	1
#Define wdSendToEmail	2
#Define wdSendToFax	3

#Define wdAlertsNone	0

lcTemplateDoc = ''

lcConnection = "Provider=VFPOLEDB;Data source="+_samples+'data\testdata.dbc'
If !File('myDummy.udl')
  Strtofile('','myDummy.udl')
Endif
m.lcSource = Fullpath('myDummy.udl')

lcSQLStatement = "select e.First_Name,e.Last_Name,e.Notes from [Employee] e"

*** 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

  If Empty(m.lcTemplateDoc) && No template
    .documents.Add()  && New file
  Else
    .documents.Add(m.lcTemplateDoc)  && Open a template
  Endif

  loDoc = .ActiveDocument
  With .ActiveDocument.Mailmerge
    .OpenDataSource(m.lcSource, wdOpenFormatAuto,.F.,,.T.,,,,,,,m.lcConnection, m.lcSQLStatement)
    .EditMainDocument	&& Activate the main document


    *!*	Add some merge fields and typ text programmatically
    .Fields.Add(.Application.Selection.Range, "Last_Name")
    .Application.Selection.TypeText(", ")
    .Fields.Add(.Application.Selection.Range, "First_Name")
    With .Application.Selection.Font
      .Italic = .F.
      .Name = 'Arial'
      .Size = 10
    Endwith
    .Application.Selection.TypeText(Chr(13)+"Here is your notes data :"+Chr(13)+Chr(13))
    With .Application.Selection.Font
      .Bold = .T.
      .Color = Rgb(0,0,255)
    Endwith
    .Fields.Add(.Application.Selection.Range, "Notes")
    With .Application.Selection.Font
      .Bold = .F.
      .Color = Rgb(0,0,0)
    Endwith
    .Destination = wdSendToNewDocument
    .Execute()

    loDoc.Close(0) && closing the template doc - keep open to create your own template

  Endwith

  .Visible = .T.   && Show word app
  .Activate
  .DisplayAlerts = wdAlertsNone
  .NormalTemplate.Saved = .T.
Endwith
**** Set the LocaleId to the previous value
=Sys(3006,Val(nlLocaleId))
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
Reply
Map
View

Click here to load this message in the networking platform