Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail merge to Word vfp 8.0

Status
Not open for further replies.

louie728

Programmer
Feb 4, 2004
27
US
Hello all,

I want to be able to do the quickest mail merge to word possible from vfp 8.0 using a table or cursor (merge to new document or printer). Right now I have a program that creates a temp word document on the fly with tables and populates the document from a vfp table. I would rather do something quicker, I noticed that vfp 8.0 has a mail merge wizard that uses mailmerge.vcx. Any help is greatly appreciated.

TIA,

Lou
 


faq184-2410?

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Lou,

When you say quicker, are you talking about run time, or the time (and effort) needed to program it?

If the former, I have had good success using bookmarks in Word rather than using the traditional mailmerge features. Essentially, you create a document with a bookmark for each variable field. Give the bookmark the same name as a field in your cursor. Then loop through the bookmarks collection, extracting the corresponding field value and stuffing it the document at the bookmark location.

It's not that difficult to do, is completely generic, and runs very quickly.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Thank you everyone for responding. I was using code from a HENTZENWERKE book (Microsoft office automation with visual foxpro) and it wasn't working out too good. I sat down and came up with the following code which is working out for me alot better. I am going to look for the bookmark possibility.


Thanks again everyone! :)


**************************************************************************
* CreateDataSource.PRG



LPARAMETERS oDoc, cCursor ,bToPrn ,cCaption &&, cDocument

#INCLUDE 'INCLUDE\APPINCL.H'


LOCAL cCSV, ; && CSV File for datasource
cIndex, ; && index of searched document
cFrmLet && Form letter

IF VARTYPE(bToPrn)="U"
bToPrn = .F.
ENDIF


cCSV = sys(2023) + "\" +"T"+ SUBSTR(SYS(2015),2)+".txt"


****************
*Create CSV file
****************
SELECT 0
SET EXCLUSIVE OFF
USE (cCursor) AGAIN IN 0 ALIAS MergeCursor
SELECT MergeCursor
*browse
COPY TO (cCSV) TYPE CSV
USE IN MergeCursor

***Get ref ot word object

TRY

oWDoc = GETOBJECT(oDoc)

**Find MM template
cIndex=GetItmInd(JUSTFNAME(oDoc))

IF cIndex = 0
cIndex = 1
endif

owdoc.Application.Documents.Item(cIndex).Activate
owdoc.Application.Documents.Item(cIndex).MailMerge.OpenDataSource(cCSV)

IF !bToPrn

owdoc.Application.Documents.Item(cIndex).MailMerge.Destination= 0 && wdSendToNewDocument

Else

With owdoc.Application.Documents.Item(cIndex).application.Dialogs(97)
.Printer = DOCPRINTER
.DoNotSetAsSysDefault = .T.
.Execute
EndWith

owdoc.Application.Documents.Item(cIndex).MailMerge.Destination= 1 && 0 sends to document 1 sends to printer

ENDIF

*/Added 2/11/04 for printing purposes will set Document (just document to printer name of choice) LA



owdoc.Application.Documents.Item(cIndex).MailMerge.Execute


IF !bToPrn
owdoc.Application.Documents.Item(1).activate()
With owdoc.Application.Documents.Item(1).application.Dialogs(97)
.Printer = DOCPRINTER
.DoNotSetAsSysDefault = .T.
.Execute
EndWith
owdoc.Application.Documents.Item(1).Windows.Item(1).Caption = cCaption
owdoc.Application.Documents.Item(1).Windows.Item(1).Visible = .T.
ENDIF
CATCH TO oERR

MESSAGEBOX("error "+oERR.message+CHR(13)+"In procedure ";
+oERR.procedure+CHR(13)+"Line #";
+STR(oERR.lineno),16,"Error")

RELEASE owdoc
ENDTRY







**Find MM template
cIndex=GetItmInd(JUSTFNAME(oDoc))
IF cIndex=0
cIndex=2
endif

owdoc.Application.Documents.Item(cIndex).Close

ERASE (cCSV)


RELEASE owdoc,cCSV,cIndex,cFrmLet,bToPrn




********************************************************************************
FUNCTION GetItmInd()
*This function recieves as a parameter the path to a file
*then scans open word documents till it finds the correct index for ref
*then returns the index #
********************************************************************************
LPARAMETERS cFname

LOCAL nDocs, ; && # of Open documents
nTempInd && index of Template

nDocs = owdoc.Application.Documents.count
nTempInd = 0
DIMENSION cDocnames(nDocs) && array of document names

FOR x=1 TO nDocs && popolate array with open word documents
cDocnames(x)= owdoc.Application.Documents.Item(x).name
IF cDocnames(x) = cFname
nTempInd = x && determining template index
ENDIF

ENDFOR

RETURN nTempInd
ENDFUNC

********************************************************************
 
Hi Louie.

>> I want to be able to do the quickest mail merge to word possible from vfp 8.0 using a table or cursor (merge to new document or printer) <<

You may want to doubload the (free) white paper and samples from my web site on Office Automation with VFP.



Marcia G. Akins
 
Thanks Marcia the white paper is very helpful. I am going to post another question about printing maybee you will be able to help....

Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top