 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Using Word to generate form letters programmatically
Using Word to generate form letters programmatically As discussed earlier, Word can be used as a powerful report writer. To illustrate this, let's go through the steps and code required to use Word as a form letter generator.
Assume that your program is being coded in Visual FoxPro (although this could have been coded in any other language that supports OLE automation). You have a table consisting of Name, Sex, Salary and Designation fields and want to print a letter as follows:
Date: Today's Date Dear Mr/Ms Name This is to inform you that you have been appointed as Designation and your gross salary is Rs. Salary The items marked in red are actual fields. The items marked in blue are derived from some other values.
Now this letter could also be generated by directly using Word's mail-merge feature. However, the mail-merge feature has some disadvantages in that it may be difficult to handle at the end-user level. Mail-merge also requires the data source to be hard-coded.
Here is how you go about building this module.
Step 1: Creating the document with form fields.
Start Word manually (not invoked from a program). Type the contents of the report as required. To insert form fields, say, the Date field, first type the word "Date:" as usual. Right click on the toolbars and choose the Forms toolbar. A Text Form Field button is available on the Forms toolbar. Position your cursor in front of the word "Date:" and click this button This will insert a small grey-coloured patch at the cursor position. Double click the grey patch to set the properties of the text form field. This following dialog box will appear. Set the values for the Date field as shown. Similarly, do this for remaining fields by specifying values as follows. Choose the formatting as required. Bookmark Type frmDate Date frmName Text frmDesignation Text frmSalary Number Step 2: Save this as a template.
Save this document using the Save As option. Use the Document Template file type. Let us assume you save it as "C:\DEMO.DOT"
Step 3: Invoke Word from within your program.
Assuming Word is being invoked from Visual FoxPro, the code to do so is as follows:
* This creates a reference to the Word application wrd = createobject('word.application') * Normally you would not want to make it visible * but for demo purposes you can view the Word file wrd.visible = .t. * Open the table (Sample) and process each row Use Sample scan * Add a new document based upon the template doc = wrd.documents.add("C:\DEMO.DOT") * Get reference to the formfields collection flds = doc.formfields The formfields available in the document are available as a collection of the activedocument object.
Step 4: Fill the correct values in all the fields.
The formfields collection is now incorporated in the variable flds. You can fill values by assigning the correct value to the result property of each field.
flds("frmDate").result = date() flds("frmName").result = sample.name * similarly, stuff values in all the other form fields Step 5: Save this document.
doc.saveas "Some file name"
This step is optional. If you do not want to store a file copy, you can omit this step.
Step 6: Print it using Word's print features.
Printing it is simple.
doc.printout * Repeat this code for each record endscan Step 7: If required, a print preview can also be incorporated.
If you want a print preview, you will have to make the Word instance visible.
* If a print preview is wanted * just make Word visible and use the command wrd.visible = .t. doc.printpriview
|
|
|