[Demos] Mail Merge

Demos, code samples. Only questions related to the existing topics are allowed here.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Demos] Mail Merge

Post by Sergey Tkachenko »

Information about and comparison of TRichView mail merge demos.
2 of them are included in demo projects, others are available as separate
downloads.

Comparison Chart for TRichView Mail Merging Demos

The recommended way to implement merge fields in our editors is Report Workshop.

Demos\Delphi\Assorted\Fields\MailMerge\
  • all fields are text items of some special text style (with the specified index);
  • fields are text items; both initial visible text of these items and tags of these items are set to field name; but visible text is not important;
  • tags will be used as field names;
  • the template editor does not save collection of styles in template documents (right-click the template editor's TRichViewEdit, choose "Settings" in the context menu. You'll see that properties are set to "Use a predefined set of styles"). So all text formatting in document is defined at design time, in RVStyle.TextStyles collection
  • the template editor's RVStyle and the main application's RVStyle have the same TextStyles collections but with one exception: the style for fields. In the template editor's RVStyle, it has a font allowing to see that it is a field clearly, in the main application's RVStyle it looks like a normal text. So, then template document is loaded, fields look like a normal text
  • FillFields procedure searches for text item with non-empty tags, and processes them as fields (changes their visible text). You can see limitations of this method:
    • field value is one line of plain text;
    • you cannot define a font of field value text in the template.
    This demo can be modified. You can set "Allow adding styles dynamically mode", and use any text styles for fields (they will be recognized by tags).
    You can use their visible text to make them look like fields (for example, it can be <<Field Name>>) and protection to disallow editing them.

Demos\Delphi\Assorted\Fields\MailMerge2\
  • all fields are text items of some special text style (with the specified index); but it's easy to modify this demo: fields can be recognized by tags, or by special properties (protection); this is because the style itself is not important - field will be deleted at the process of mailmerge;
  • this demo does not use tags
  • template editor saves collections of styles in template documents (right click the template editor's TRichViewEdit, choose "Settings" in the context menu. You'll see that the properties are set to "Allow adding styles dynamically"). This allows to add your own styles, implementing commands like "Bold", "Font", using RichViewActions, etc.
  • ReplaceFields deletes fields and inserts field values at their place; during this process, not only styles of the template are used, but also styles from documents representing field values.
    You can see limitations of this method: fields are deleted at the process of mailmerge; if you need to perform this procedure for the second time, you need to reload template.
    But this is the only method allowing to have arbitrary documents as field
    values.

MailMerge3
mailmerge3.zip
(4.86 KiB) Downloaded 8322 times
  • Field values are multiline text.
    It works absolutely like MailMerge2, but field values are text, not RVF.
    Has limitations of both MailMerge and MailMerge2, but simpler than MailMerge2.
    This demo requires BDE.
[+] Old versions of this demo
https://www.trichview.com/support/files/mailmerge3.zip - for TRichView 17.2 and older

Mailmerge-LabelItem (simple version)
mailmerge-labelitem.zip
(7.13 KiB) Downloaded 8107 times
  • fields are not text, but special "label item"; label-items show read-only one line of text, unwrappable;
  • field name is stored in tags; initial text of label-items is not important;
  • you can use "Allow adding styles dynamically" mode with all its bonuses (not shown in this demo);
  • FillFields procedure searches for label items, reads their tags and changes their text; font of "label-items" is not changed, so you can define it in the template editor.
  • Limitations:
    • field values - one line of text
There is an advanced version of this demo in Demos\Delphi\Assorted\Fields\MailMerge-LabelItems\

[+] Old versions of this demo
https://www.trichview.com/support/files ... elitem.zip - for TRichView 17.2 and older

MailMerge-FreeStyle
mailmerge-freestyle.zip
(7.56 KiB) Downloaded 8256 times
  • It's like Demos\Delphi\Assorted\Fields\MailMerge\ demo, but does not use a fixed set of styles.
    Styles for fields are created on the fly, so fields can have different fonts.
[+] Old versions of this demo
https://www.trichview.com/support/files ... estyle.zip - for TRichView 17.2 and older

MailMerge-Text
mailmerge-text.zip
(6.61 KiB) Downloaded 8210 times
  • This demo is the same as in Demos\Delphi\Assorted\Fields\MailMerge\, but fields are defined as text between { }
[+] Old versions of this demo
https://www.trichview.com/support/files ... e-text.zip (for TRichView 17.2 and older)

Read also comments at the beginning of the these demos' main form units.

[+] History of updates
2008-Dec-11: for compatibility with TRichView 11 and Delphi 2009.
2011-Oct-2: for compatibility with TRichView 13.4
2018-Apr-28: for compatibility with TRichView 17.3
Last edited by Sergey Tkachenko on Sun Oct 02, 2011 7:20 pm, edited 6 times in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

MailMerge-Text2
mailmerge-text2.zip
(4.56 KiB) Downloaded 8094 times
Almost the same as MailMerge-Text demo, but generates one multipage report instead of individual reports for each records.
A template editor and a report generator are combined in a single application.

Field codes: text in {}, for example {NAME}.
Field values: single line text.

[+] History of updates
2008-Dec-11: for compatibility with TRichView 11 and Delphi 2009
2018-Apr-28: for compatibility with TRichView 17.3
[+] Old versions
https://www.trichview.com/support/files ... -text2.zip - for TRichView 17.2 and older
Last edited by Sergey Tkachenko on Thu Dec 11, 2008 1:55 pm, edited 2 times in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

MailMerge-Text3
mailmerge-text3.zip
(6.55 KiB) Downloaded 8318 times
The same as MailMerge-Text2, but allows multiline field values.

Field codes: text in {}, for example {NAME}.
Field values: multiline text.

Note: see also the scripting demo, viewtopic.php?t=9
mailmerge-text3-2.zip
(6.56 KiB) Downloaded 8257 times
The same demo, but field codes are identifiers started from $, for example $NAME.

[+] History of updates
2008-Dec-11: updated for compatibility with TRichView 11 and Delphi 2009
2018-Apr-30: updated for compatibility with TRichView 17.3. Tab characters in field values are supported. Unicode field values in all versions of Delphi.
[+] Old versions
https://www.trichview.com/support/files ... -text3.zip - {}-codes, for TRichView 17.2 and older
https://www.trichview.com/support/files ... ext3-2.zip - $-codes, for TRichView 17.2 and older
Last edited by Sergey Tkachenko on Wed Jun 15, 2011 6:35 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

MainMerge-Text4
mailmerge-text4.zip
(8.86 KiB) Downloaded 8338 times
The same as MailMerge-text3, but supports several types of field values.

Field codes: text in {}, for example {NAME}.
Field values: multiline text / picture / table / arbitrary document (RVF)

[+] History of updates
2015-Mar-26: RVF field type is added
2018-Apr-30: compatibility with TRichView 17.3. Unicode text in all versions of Delphi. Tab characters are allowed in text field values.
[+] Old versions
https://www.trichview.com/support/files ... -text4.zip - for TRichView 17.2 and older
Last edited by Sergey Tkachenko on Wed Mar 25, 2015 10:41 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

GeoReport
GeoReport.zip
(235.82 KiB) Downloaded 8256 times
Field codes: arbitrary fragment.
Field values: either one line of text or arbitrary RVF document.

New field values can be applied without template reloading (old field values can be replaced with new field values).

In this demo, a field is a set of adjacent items (of any type) marked with the same code. Field code is stored in items tags, in the form "Code|" for normal items, or "Code|Target" for hyperlinks.

This demo supports two sources of field values:
1) an arbitrary document stored in RVF format in TBlobField
2) a single line text stored in TStringField.

This demo consists of 2 projects:
- DataEdit: editing database of field values;
- GeoReport: the main applications.

Two versions of this demo are included
1) BDE version, with Paradox table
2) FireDAC version, with InterBase table (the table was created using InterBase 2017)

Showing field codes:
Image
Showing field values:
Image

Sample data were taken from Wikipedia.


[+] History of updates
2010-Oct-21: fix related for using tags in field text (hyperlinks)
2011-Oct-2: compatibility with TRichView 13.4
2014-Mar-13: compatibility with TRichView 14.x
2018-May-1: compatibility with TRichView 17.3; new FireDAC+InterBase version of this demo. Values of plain text fields are colored using colors of adjacent text, when possible. Unicode in all versions of Delphi. Using a hidden placeholder for empty field values, to prevent losing fields.
[+] Old versions
https://www.trichview.com/support/files ... t_v1_1.zip - for TRichView 17.2 and older
Last edited by Sergey Tkachenko on Thu Mar 13, 2014 9:51 am, edited 4 times in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

In the demos above, when the field value is a multiline plain text, this value cannot contain tab characters.

To fix this problem, replace InsertMultilineText function in these demos to the function from this file:
http://www.trichview.com/support/files/ ... thtabs.zip


Updates:
2018-Apr-30: the demo presented in this post is for TRichView 17.2 and older. All other demos in this topic that replace {}-codes with multiline text were updated for compatibility with TRichView 17.3 and support tab characters too.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Master-detail
https://www.trichview.com/support/files ... detail.zip

This demo, like mailmerge-text4, supports multiline text and picture fields. Fields are defined as text inside {}.
The main difference:this demo supports a master-detail relationship.
A template in this demo may contain tables linked to datasets. Such tables have a repeated group of rows; they are repeated as many times as many records in the linked dataset. If such table is inserted in a repeated row of another table, a master-detail relationship is established. Tables can be nested on any depth. The demo shows 3 levels of tables.

Image
Detailed comments are at the beginning of the main form's unit.

There are two versions of this demo:
1) FireDAC with InterBase tables (the table is created in InterBase 2017)
2) BDE with Paradox tables

This demo is not universal. Master-detail relationships are hard-coded in the application (the application knows that the tables KINGDOM, PHYLUM and CLASS have a master-detail relationship, and assign filters accordingly).
If you need an universal solution for building master-detail reports, check out our Report Workshop components.

[+] History of updates
2010-Oct-13: 0 matched records in a detail table is processed correctly.
2018-May-1: Compatibility with TRichView 17.3. New FireDAC + InterBase version of this demo. Picture format auto-detection (previously, only bitmaps were supported).
[+] Old versions
https://www.trichview.com/support/files ... il-old.zip - for TRichView 17.2 and older (BDE only)
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Protect-fields demo:
protectfields.zip
(3.66 KiB) Downloaded 8154 times
This demo does not actually make a mail merging.
It shows how to protect text fields.
When the user clicks on the "Protect" button, it searches for all occurrences of text inside {} and apply a special (protected) style to them.

This feature is useful, for example, if you want to have fields protected and store documents in RTF.

[+] History of changes
2018-May-1: text is processed as Unicode for all versions of Delphi.
icehouse
Posts: 3
Joined: Tue Jan 12, 2010 9:29 pm
Location: Essen

How to merge 2 different e-mails with 2 different layouts?

Post by icehouse »

Dear Sergey,
I am writing on an e-mail program for some time. It is working fine but I have problems to merge e-mails with 2! different e-mail layouts if I want to reply to an received e-mail. I spend a lot of time to achive this but I am not satisfied about the result...

The original e-mail has its own styles. As you know this is sometimes with CSS, sometimes inline CSS, sometimes direct html tags.

The standard reply layout may contain different fonts, colors and styles in inline CSS (I am using trichviewedit for this with rvhtmlviewimport)

In the beginning I replaced just the placeholder (originalmail) with the complete e-mail. Of course any layout was either lost or the layout was used from the reply layout...

Furthermore this has to be possible without creating trichtview on a form. Somehow this is crashing as soon as I call Richview.format beause the scrollbars seem to generate a problem as the need an owner which is nothing.
The function should do:
1. read orginal email as html with images (provided as streams. At the moment I am using thtmlviewer...)
2. SaveRVFToStream
3. read layout of reply with images
4. merge RVF with previous RVF stream
5. return new RVF stream with images.

Do you have an example for this case or maybe have an idea on how to do this easier?

Many thanks in advance!
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Demos\DelphiUnicode\Assorted\Fields\MailMerge2\ shows how to replace a field to RVF content.
icehouse
Posts: 3
Joined: Tue Jan 12, 2010 9:29 pm
Location: Essen

Post by icehouse »

Thank you for the quick answer.
Yes, I have tested MailMerge2, too but it does not work if TRichViewedit is not placed on a tform. Can you test this?
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Only call of Format requires a parent form.

Workaround for this problem:
1) Assign some form as a parent, but make the component hidden
or
2) Use TRVReportHelper.RichView. TRVReportHelper is formatted using Init method, and it does not require a parent form.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Demos] Mail Merge

Post by Sergey Tkachenko »

MailMerge-ColoredText

One more mail merging demo:
mailmerge-coloredtext.zip
(7.26 KiB) Downloaded 8283 times
(requires TRichView 17.3 or newer)

Field codes: text in {}, for example {NAME}.
Field values: multiline text, optionally with the specified color.

PS: In Report Workshop, you can define text color in field format strings and using $if commands.

[+] History of updates
2018-Apr-30: changed for compatibility with TRichView 17.3. Tab characters in values are supported. Unicode field values in all versions of Delphi.
[+] Old versions
https://www.trichview.com/support/files ... edtext.zip - for TRichView 17.2 and older
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Demos] Mail Merge

Post by Sergey Tkachenko »

I have updated all demos in this topic for compatibility with TRichView 17.3

Additional changes:

1. All MailMerge-Text demos that support multiline text now use the same InsertMultilineText procedure. This procedure is moved to RVInsertMultilineText.pas. This unit is included in the demo projects, you can insert it in your application.
This new version of InsertMultilineText is universal:
- it is Unicode for all versions of Delphi;
- it supports tab characters in field values
- it has an optional StyleNo parameter, so you can define a special font attributes for field values (this feature is used only in MailMerge-ColoredText demo)

2. GeoReport and Master-Detail demos are now available not only as BDE + Paradox versions, but also as FireDAC + InterBase versions.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Demos] Mail Merge

Post by Sergey Tkachenko »

MailMerge-LabelItems-And-Images
mailmerge-li-and-images.zip
(58.67 KiB) Downloaded 8090 times
MailMerge-Labels-and-Images.png
MailMerge-Labels-and-Images.png (23.75 KiB) Viewed 175963 times
This is an improved version of the demo that can be found in <TRichView Dir>\TRichView\Demos\DelphiUnicode\Assorted\Fields\MailMerge-LabelItems\
It has two types of fields:
- label items for text fields
- pictures for image fields.

Field codes are stored in item tags.

On field merging, no items are added/deleted, only their content is modified. Because of this, it's not necessary to reload a report template to apply another set of fields.

Avatars are designed by Freepik
Post Reply