<< Click to Display Table of Contents >> Navigation: Programming > Mail Merge (replace fields with data) and data forms > Update Field (Insert Text from Database) > Start Merge process |
The mail merge is started by the method
MergeText(const FieldName: string = ''; AllTexts: Boolean = FALSE).
Both parameters are optional. If you need to merge only the text the cursor is located in (header, footer ..) use procedure MergeActiveText;
Example:
MergeText('',true) - merge the fields in all areas, headers and footers
MergeText('A*', true) - merge the fields whith names starting with "A" in all areas, headers and footers
MergeText('A*', 'name', true) - merge the fields whith names starting with "A" and Source='name' in all areas, headers and footers
Important:
If you need to print (or export to PDF) the text right after the merge process you need to call ReformatAll(false, true)!
If you need to merge the fields only in a header or footer text use the MailMerge procedure of any TWPRTFDataBlock.You will have to pass the OnMailMergeGetText procedure described below as callback.
Example:
Merge fields only in all header texts:
for i:=0 to WPRichText1.HeaderFooter.Count-1 do
if (WPRichText1.HeaderFooter[i].Kind=wpIsHeader) and // only header
not (WPRichText1.HeaderFooter[i].IsEmpty) then // and not empty
WPRichText1.HeaderFooter[i].MergeText(
WPRichText1, // object used as 'Sender', can be also datasource ...
WPRichText1MailMergeGetText, // event handler (see below)
false, // readonly, we want to modify
'', nil, nil ); // optional to restrict merge range