Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
OLE -- WHAT AM I DOING WRONG??
Message
From
17/03/1998 06:10:04
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
16/03/1998 23:37:06
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Miscellaneous
Thread ID:
00084951
Message ID:
00084972
Views:
45
>hello what is wrong with the following code:
>
>code starts here
>
>* use filename && calls for a database
>set textmerge on
>set textmerge delimiters to
>i=1
>go 1
>do while i<=999999
>@ 10,10 say " Processing rec # -->"+str(i)
>set textmerge to D:PCLTEST.PRT noshow additive
>TEXT
><>
>6200 South Syracuse Street, Suite 150
>Englewood, CO 80111
>
>TEL: (303) 221-2000 FAX: (303) 221-3000
>
>ENDTEXT
>set textmerge to && closes the text file open
> && thru TEXTMERGE
>** OLE is here ************************
>oWorg=createobject("word.basic")
>if i<=1 && loops thru main file, first instance of word
>WITH oWorg && opens an instance of Word97
>.fileopen("d:\pcltest\test1.doc") && file created and to be used as an "overlay"
>.FILEPRINT(1,1,0,"d:\pcltest\pcltest.prt",,,,,,,,1,,)
>endwith && assuming Word97 and a HP 5si laser
>endif
>if i>1 && <--- error line here
>WITH oWorg
>.fileopen("d:\pcltest\test1.doc") && file created and to be used as an "overlay"
>.FILEPRINT(1,1,0,"d:\pcltest\pcltest.prt",,,,,,,,1,,)
>endwith
>endif
>
>IF EOF()
>SKIP -1
>I=99999999
>ELSE
>skip
>i=i+1
>ENDIF
>enddo
>
>set textmerge to
>
>*************
>RELEASE ALL
>************
Hello Mark,
Checked my mail before here and sent you reduction + mailmerge way. Will paste here too for those who can benefit as is.

Hi Mark,
How is it going.
As I could understand you're trying to get text and data per rec into a
template and print it.
This code would be so slow for you create a new word object in loop. That
would at last stop the system and I didn't run it for that reason. The error
was related with that I think. Supposing it would exactly as you want and
with textmerge I reducted the code a little. At the attachment there is
another way to do it with mailmerge. It's easier to manage and faster
Took less than 10 secs for 99 records customer.dbf when merged to a new
document - To printer would be fast too. You could preset your merge file by
running with .filenewdefault once for mailmerge main doc, later change to
filenew(cTemplateName). In this case don't forget to rem fileexit(2) and
remove rem for .appshow :
*cText =  "    6200 South Syracuse Street, Suite 150"+chr(13)+;
*             "    Englewood, CO 80111"+chr(13)+;
*             "    TEL: (303) 221-2000  FAX: (303) 221-3000"


* use filename    && calls for a database
set textmerge on
set textmerge delimiters to
go top
oWorg=createobject("word.basic")   && Create just one instance
scan
   @ 10,10 say " Processing rec # -->"+str(recno())
    set textmerge to D:PCLTEST.PRT noshow additive && Better use fullpath
    TEXT
    <<i>>
    6200 South Syracuse Street, Suite 150
    Englewood, CO 80111

    TEL: (303) 221-2000  FAX: (303) 221-3000

    ENDTEXT
    set textmerge to
* cVar = ltrim(str(recno()))+chr(13)+cText
    WITH oWorg
        .filenew("d:\pcltest\test1.doc")     && file created and to be used
as an "overlay" is template
* Instead of textmerge - here directly using insert is better,
* if data in memo or assigned to cVar it's easier to print with one insert
*        .insert(cVar)
* Then only .fileprint
        .FILEPRINT(1,1,0,"d:\pcltest\pcltest.prt",,,,,,,,1,,)
        .fileclose(2)           && Close w/o save
    endwith
    if recno() > 999999   && If 999999 is really needed
       exit
    endif
endscan
oWorg.appclose        && Close word
* oWorg.fileexit(2)      && Same

RELEASE ALL
****************
End of code
Have a nice day
Cetin

And here is the attached code :
**************************************************
* Start of code
**************************************************

PUBLIC oWordDocument

*** set the LOCALEID to English
nlLocaleId=sys(3004)		&& Save local id
=sys(3006,1033)				&& We will be sending instructions in English 
wait window nowait "Creating Word Document..."     && Inform user
oWordDocument=createobject("word.basic")	&& Create word object
with oWordDocument
* Remove the rem if you want the user see what's going on in word
*	.appshow							&& Show word app - Word 7.0 support
	.filenewdefault        				&& New file, default template - Will create datasource
	cDSN = "DSN=Visual FoxPro Tables;UID=;PWD=;"
	cSourceDb = "SourceDB=C:\Program Files\vfp50\SAMPLES\DATA\Testdata.dbc"
	cSourceType = ";SourceType=DBC;"
	cOther = "Exclusive=No;BackgroundFetch=No;Collate=Machine;"

	cSQLStatement = "SELECT recno() as rcno,* FROM customer"

* Insertdatabase parms build here for clarifying
* 35,511 are autoformatting options for grid - ignore
* 0 Link to source false

	.InsertDatabase(35,511,0, ;
		cDSN+cSourceDb+cSourceType+cOther, ;
		cSQLStatement,,,,,,,1)

	.filesaveas("c:\temp\xxmmergedoc",0)			&& Save as a word doc for mailmerge
	.fileclose(1)						&& Close saving
* Datasource ready, now create mailmerge main document
	.filenew("c:\temp\prttest.doc")     && New file based on prttest (Logo in header - address in footer)
* If you have a document template with logo in header&footer etc use it else .filenewdefault
	.MailMergeOpenDataSource("xxmmergedoc.doc")  && Set saved file as data source for mailmerge (Directly a table could be set via ODBC)
	.MailMergeEditMainDocument			&& Activate the main document
	.insertmergefield("rcno")
	.insertpara
	.Insert("Dear ")                    && Start writing
	.InsertMergeField("Contact")        && Insert a merge field - any field in data table
	.Insert(",")
	.InsertPara							&& Or +chr(13) to previous
	.Insert("Some text here...")
	.InsertPara
	.Insert("Thanks.")
	.InsertPara
	.Insert("Yours sincerely")
	.InsertPara
	.InsertPara
	.insert("6200 South Syracuse Street, Suite 150"+chr(13))
	.insert("Englewood, CO 80111"+chr(13))
	.insert("TEL: (303) 221-2000  FAX: (303) 221-3000"+chr(13))
* .MailMergeToDoc       && Merge to a new document - For printer .MailMergeToPrinter
* .FileSaveAs("c:\temp\frmletters.doc") && If want to save
	.MailMergeToPrinter
	.Fileexit(2)   && Exit no save - directly print and exit w/o save
endwith
* InsertWordField could also be used but FP is more capable of it so we should have transfered the needed already
* Here the control is passed to word. Sitting and waiting for further process.
wait window "Data Merged to c:\temp\frmletters.doc"

**** Set the LocaleId to the previous value
=sys(3006,val(nlLocaleId))

**************************************************
* End of code
**************************************************
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