Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Help with Word Automation
Message
 
À
06/08/2001 22:37:28
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Divers
Thread ID:
00540510
Message ID:
00540598
Vues:
18
James...

Heres a method I've taken directly out of one of my apps... it performs a mail-merge based on a mail-merge letter and some foxpro table source...
I'm not using any #DEFINEs here as would be available in a Word header file, but at least the variables should be easy to follow the function calls.

HTH.
** Mail merge Letter/Label file
** Mail merge Data Source file
lParameters lcMMLetterFile, lcMMDataFile

local lcErrorHold
lcErrorHold = on( "error" )
llObjOk	= .T.
on error llObjOk = .F.

oWord	= getobject( "", "Word.Application" )
if type( "oWord.Name" ) <> "C"
	lcMsg	= "I am unable to load/locate Word 2000" +chr(13);
			+ "to complete mail merge process."
	Messagebox( lcMsg, 0, "Unable to merge" )
	return
endif

*/ Don't keep re-opening multiple instances of Word...
*/ use existing...  USE OBJECT BROWSER... Shows all
*/ word crap, parameters, objects, properties, methods, etc
*/ Parameters to Word, logical True/False values
*/ are numeric based... 0 = FALSE
lcFileName		= lcMMLetterFile
lnConfirmConversions	= 0
lnReadOnly		= 0
lnAddToRecentFiles	= 0
lcPasswordDocument	= ""
lcPasswordTemplate	= ""
lnRevert		= 0
lcWritePasswordDocument	= ""
lcWritePasswordTemplate	= ""

if llObjOk
	oWord.Documents.Open(;
		lcFileName,;
		lnConfirmConversions,;
		lnReadOnly,;
		lnAddToRecentFiles,;
		lcPasswordDocument,;
		lcPasswordTemplate,;
		lnRevert,;
		lcWritePasswordDocument,;
		lcWritePasswordTemplate )
endif

lcRootPath	= left( lcMMDataFile, rat( "\", lcMMDataFile ) -1 ) + ";"
lcDataFile	= substr( lcMMDataFile, rat( "\", lcMMDataFile ) +1 )
lcName		= lcMMDataFile
lnConfirmConversions	= 0
lnwdOpenFormatAuto	= 0
lnReadOnly		= 0
lnLinkToSource		= 1
lnAddToRecentFiles	= 0
lcPasswordDocument	= ""
lcPasswordTemplate	= ""
lnRevert		= 0
lcWritePasswordDocument	= ""
lcWritePasswordTemplate	= ""
lcConnection		= "DSN=FoxPro Files - Word;UID=;PWD=;";
			+ "SourceDB=" + lcRootPath ;
			+ "SourceType=DBF;Exclusive=No;";
			+ "BackgroundFetch=Yes;Collate=MACHINE;";
			+ "Null=Yes;Deleted=Yes;"

lcSQLStatement		= "SELECT * FROM " + lcDataFile
lcSQLStatement1		= ""

*/ If no errors yet by the above opening document,
*/ object references, etc trapped by ON ERROR...
if llObjOk
    oDoc = oWord.Documents(1)
endif

if llObjOk
    oDoc.MailMerge.OpenDataSource(;
		lcName,;
		lnwdOpenFormatAuto,;
		lnConfirmConversions,;
		lnReadOnly,;
		lnLinkToSource,;
		lnAddToRecentFiles,;
		lcPasswordDocument,;
		lcPasswordTemplate,;
		lnRevert,;
		lcWritePasswordDocument,;
		lcWritePasswordTemplate,;
		lcConnection,;
		lcSQLStatement,;
		lcSQLStatement1 )
endif

if llObjOk
	With oDoc.MailMerge
	    .Destination		= 0  && to new file...
	    .MailAsAttachment		= 0
	    .MailAddressFieldName	= ""
	    .MailSubject		= ""
	    .SuppressBlankLines		= 1
	    With .DataSource
	        */ wdDefaultFirstRecord  1
		*/ wdDefaultLastRecord  -16
	        .FirstRecord	= 1
	        .LastRecord	= -16
	    EndWith
	    .Execute(0)
	EndWith
endif


*/ Now, print it...
lnBackground		= 1
lnAppend		= 0
lnRange			= 0
lcOutputFileName	= ""
lnPageFrom		= 1
lnPageTo		= 5
lnItem			= 0
lnCopies		= 1
lcPages			= ""
lnPageType		= 0
lnPrintToFile		= 0
lnCollate		= 1
lcFileName		= ""
lnActivePrinterMacGX	= 0	
lnManualDuplexPrint	= 0	
lnPrintZoomColumn	= 0
lnPrintZoomRow		= 0
lnPrintZoomPaperWidth	= 0
lnPrintZoomPaperHeight	= 0

*/ disabled this to automatically print document
*oWord.Application.PrintOut()


*/ This would force close the word document
*/ Parameter = 0 means don't prompt to save changes...
* oWord.Documents.Close(0)

*/ But I just changed it to VISIBLE = .T. so users
*/ could pre-review the output before final printing
*/ and make necessary edits beforehand.
oWord.Visible	= .T.

*/ Restore error handler
on error &lcErrorHold

return
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform