Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word Automation
Message
From
25/02/1999 05:40:17
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
00191231
Message ID:
00191319
Views:
107
>I need to do a Word Merge.
>
>I have so far been succesfull at
>getting a single record to print
>from VFP into Word 97 as in a form
>letter using the following syntax:
>
> oWordObj.ww7_EditGoto ("contact")
> oWordObj.Insert (TestData.Contact)
>
>This code works fine with predefined
>bookmarks in Word. Can anyone help me
>with sending multiple rows of data.?
>
>How do I get records from a VFP table
>to merge into Word in a tabular format
>as in:
>
> HEADER1 HEADER2 HEADER 3
>----------------------------------------
> Column1 Data Col2 Data Col3 Data
> Column1 Data Col2 Data Col3 Data
> Column1 Data Col2 Data Col3 Data
>
>
>
>Thanks in advance
Hi Kevin,
Mailmerge class in files\classes section is updated. But this is not truely a mailmerge rather an "insertdatabase", "converttexttotable" or alike operation. There are numerous ways to do it :
- Modify mmerge method of mailmerge class to "insertdatabase" instead of mailmerge.
- Export data to xls and import xls in word (who needs it)
- Datatoclip and paste to word, converttexttotable and so on.
Some are short in code and fast but lack of capabilities such as sending memo fields to word also. Whatever you use I would suggest using word.basic (or wordbasic object of word.application) because commands to achieve this purpose are compatible word.basic commands for both word95 and word97. Here is a modified version of mmerge method (this can send memo fields too) :
=mmerge([select company, contact, title, address, city from (home()+"SAMPLES\DATA\customer") ])

function mmerge
lparameters tcSQL, tcTemplate, tnDestination, tlShow, tlWaitWordFinish
tlWaitWordFinish = iif(parameters()<5, .T., tlWaitWordFinish) && tlWaitWordFinish default .T.
if type("tnDestination") # "N"
   tnDestination = 0
endif   
if type("tcTemplate") # "C" ;
   or !file(tcTemplate)
   tcTemplate = ""
   tnDestination = 0
   tlShow = .t.
endif
if tnDestination # 2
   tlShow = .t.
endif

#define LOC_NORECTOREPORT	"No records to report !" 
local lcAlias, lcDefDir,cDSN,cSourceDB,cSourceType,cOther,cSQLStatement,cDataSource, ;
		lcFreeTable,lcAlias,lcFreeTableName, lnReccount, lnWordWindow

lcAlias = alias()		&& Save current alias if any

lcFreeTableName = "F"+right(sys(2015),7) && Create a temp table name
* Word doesn't like table names that are not in 8.3 format

&tcSQL into table (lcFreeTableName)	&& Select recs into a temp table

lcFreeTable = alias()
lcDefDir = sys(5)+curdir()
* Prepare DSN strings
cDSN = "DSN=FoxPro Files;DBQ="+lcDefDir+";DefaultDir="+lcDefDir+";"
cSourceDB = ""
cSourceType = ""
cOther = "DriverId=536;MaxBufferSize=512;PageTimeout=5;"
cDataSource = dbf(lcFreeTable)
cSQLStatement = "SELECT * FROM "+lcFreeTableName+".dbf"
lnReccount = reccount()
cTablePath = dbf()
use
if lnReccount = 0
	messagebox(LOC_NORECTOREPORT)
	erase (lcFreeTableName+".*")
	return
endif	


PUBLIC oWordDocument

#define autoformat_None 0
#define autoformat_Borders 1	
#define autoformat_Shading 2	
#define autoformat_Font 4	
#define autoformat_Color 8	
#define autoformat_AutoFit 16	
#define autoformat_HeadingRows 32	
#define autoformat_LastRow 64	
#define autoformat_FirstColumn 128	
#define autoformat_LastColumn 256	

#define NoLinkToSource 0
#define LinkToSource 1
#define TableAutoformatType 9
* Colorful 2 - These are enumerated in the order you see \Table\Autoformat menu

*** 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
	if !empty(tcTemplate)
		.filenew(tcTemplate)	&& open using template table
	else
		.filenewdefault			&& no template - new file
	endif
	.InsertDatabase(TableAutoformatType,;
		autoformat_Borders + autoformat_Shading + autoformat_Font + autoformat_Color + autoformat_Autofit + autoformat_HeadingRows ;
		,NoLinkToSource, ;
		cDSN+cSourceDb+cSourceType+cOther, ;
		cSQLStatement,,,,cTablePath,,,1)
	.appshow									&& Show word app - Word 7.0 support
	wait clear
	if !tlShow					&& No show
		.fileexit(2)			&& Exit no save
		release oWordDocument
	else
		.appshow				&& Show word app
		DECLARE integer GetForegroundWindow in WIN32API
		lnWordWindow = GetForegroundWindow()	&& Save word window handle
	endif	
ENDWITH


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

wait clear	&& Clear wait windows if any
if type("oWordDocument") # "U"	&& oWordDocument still alive
	if	tlWaitWordFinish 		&& Should we wait it to finish
		DECLARE short IsWindow in WIN32API integer
		do while IsWindow(lnWordWindow)	# 0 && Loop while our word window alive
		enddo
		release oWordDocument
		erase (lcFreeTableName+".*")
	else
		return (ltrim(str(lnWordWindow))+"%"+lcFreeTableName)	
		&& Return window handle and temp table name. 
		&& So caller might deal with it later	(handle and tablename is separated with %)
	endif
endif
clear dlls	
if !empty(lcAlias)
	select (lcAlias)
endif
If you won't use memo fields then DataToClip could be the fastest method.
PUBLIC oWordDocument

*** set the LOCALEID to English
nlLocaleId=sys(3004)		&& Save local id
=sys(3006,1033)			&& We will be sending instructions in English 
select company,contact,title ;
  from (home()+"samples\data\customer") ;
  into cursor test
application.datatoclip("test",reccount(),3) && Copy to clipboard delimited with tabs
wait window nowait "Creating Word Document..."

oWordDocument=getobject("","word.basic")	&& Create word object-if already running get it
with oWordDocument
	.appshow			&& Show word app - Word 7.0 support
	.filenewdefault     && New file, default template
	.toggleportrait     && Toggle page orientation (This is a toggle !) Portrait -> Landscape
	.editpaste          && Paste clipboard data
	.EditSelectAll      && Select entire doc
	.TextTotable(1)		&& Convert text to table - tabs (1) (Word table)
	.editgoto("\Table") && Find the table
	.nextcell
	.prevcell           && Trick to go to first cell and select - should be an easier way :)
    for ix= 1 to fcount() && Skip header row-replacing header values with fieldnames
       .insert(field(ix)) && VFP pasted clipdata padded with spaces - not sutable for merging
	   .nextcell
	endfor
	.TableDeleteColumn  && Delete extra column
endwith
wait clear
**** 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