Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word automation and VFP.
Message
From
10/09/2004 06:33:55
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
09/09/2004 17:02:18
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00940838
Message ID:
00940960
Views:
571
>Hello,
>
>I am trying to create a mail merge document and add one text and one field from a file called merge.dbf. I run the following code in VFP6 (moving to 8)
>and get a dialog box asking to "Select Data Source". Here is the code:
>
>#Define MYDBC "c:\proj2000\data\merge.dbf"
>
>cSQLStatement1 = "SELECT * FROM Merge"
>cSQLStatement2 = ""
>
>oWordDocument=createobject("word.application") && Create word object
>With oWordDocument
> .Documents.Add && Use a predefined template if u wish
> With .ActiveDocument.MailMerge
> .OpenDataSource(,,,.T.,,,,,,,, ;
> "DSN=Visual FoxPro Database;SourceDB="+MYDBC+;
> ";SourceType=DBF;Exclusive=No;",;
> cSQLStatement1, cSQLStatement2) && Open data source - Link to source
> .EditMainDocument && Activate the main document
> .Application.Selection.TypeText("First Name :")
> .Fields.Add(.Application.Selection.Range,'firstname')
> .Application.Selection.TypeText(CHR(13)+"Blah blah ...."+Chr(13))
> ENDWITH
> .Visible = .t.
> .Activate
>EndWith
>
>Any ideas what I am doing wrong? I need something VERY simple.
>
>Thanks,
>
>-Greg

Greg,
Your code looks familiar and you're doing nothing wrong (except upgrading word:) - actually a slight error but it's not the culprit)
Word's mailmerge with foreign sources like VFP tables is simply trouble. You write code perfectly working but then comes out word2XXX breaking backward compatibility or it's new version of MDAC spoiling it. I got my lesson with word and now never using OpenDataSource if the source is not an Office member (word,excel..). Free from hassle version (source is now a tab delimited text file how word knows to open).
Select * From customer Where country='USA' Into Cursor mergethese
MergeIt('mergethese')

Function MergeIt
Lparameters  tcAlias, tcTemplateDoc
Local nLocaleID,lcTemp,lnFields,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'
lnFields = Fcount()
Copy To (lcTemp) Type Delimited With "" With Tab
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

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