MS-Word Mail Merge component for Delphi 5

MS-Word Mail Merge component for Delphi 5

Description
Requirements
Sample scenario
Download the freeware component
About the author

1. Description

I made this small component because I needed mail merge capabilities in an application, and I couldn't find a free component on the internet. The component has been tested in Word'97 and Word 2000.

Via this component, your application can easily create Word documents with application generated contents. The component will open a Word template file (document), and link (merge) the field data from your application to a new generated document.

2. Requirements

The component requires Delphi 5, and the Delphi 5 Office Automation Servers. The office automation servers will be used internally by the component.

3. Sample scenario and code

Suppose that you have a database application with a customer table, a product table, and a sales table. You run a query which shows all customers which have ordered something, but they have not paid there bills yet. Now you want to send them a letter (a real paper letter in an enveloppe) to inform them about there depts.
Follow these steps to accomplish the task:
  1. Start Word, and open or create a document.
  2. Insert mailmerge fields on locations where you want database fields: menu "Insert -> Field. A form appears with 2 listboxes and an input field which contains the text "=". Replace the "=" by the text "Mergefield FirstName". Firstname is your own field name, we will use this fieldname in the delphi application. Close the form by activating the "OK" button. The text <> appears in your document.
  3. The rest of the Word stuff is easy now you know how to insert fields in your document. Finish up your document with some more fields and text, and close it.
  4. Put the MailMerge unit in the Uses list of your Delphi unit.
    Create the component and setup the properties. Set the 'columns' property to something like 'firstname;lastname;amountdue;employeename'. Add all records from your query to the component:
        MailMerge.Columns := 'firstname;lastname;amountdue;employeename';
        YourQuery.First;
        while not YourQuery.EOF do
        begin
          MailMerge.Append;
          MailMerge('firstname') := sqlYourQueryFirstname.Value; // (from a TStringField component)
          MailMerge('lastname') := sqlYourQueryLastname.Value;
          MailMerge('employeename') := tblSession.EmployeeName;
          //etc
        end;
        
  5. Set the document name property in your component, eg:
        MailMerge.DocumentName := 'c:\my documents\test merge.doc';
      
  6. Optionally, set the OnStatusMessage event:
        MailMerge.OnStatusMessage := MailMergeStatusMessage;
        .....
        procedure MailMergeStatusMessage(Sender: TObject);
        begin
          StatusBar1.SimpleText := TMailMerge(Sender).StatusMessage;
        end;
      
  7. Start the mail merge:
        MailMerge.Merge;
      
    Word will launch, and a temporary datasource document will be generated by the component. The template will opened and merged with the datasource document. The result comes in a new document, and can be accessed via the component's WordDoc interface. (Eg: MailMerge.WordDoc.Print and MailMerge.WordDoc.Close)

4. Download the freeware component

MailMerge.zip can be downloaded here

A demo application is included. There is no 'register' procedure, just create the component runtime or add your own register function...

5. About the author

My name is Robert van Tilburg and I live in the Netherlands. I'm 21 years old and a computer science student. I learned programming on a MSX-1 homecomputer when I was 10 years old. Now I have various part-time Delphi programming jobs, and experience with Delphi, Turbo Pascal, Basic, 32 bits assembler, C++, C++ Builder, Databases (paradox, Access, SQL Server via BDE and ADO), DirectX, Internet, and desktop applications.

EMail: lothar@lucrasoft.nl