<< Click to Display Table of Contents >> Navigation: Programming > Mail Merge (replace fields with data) and data forms > Customize Field Display |
The merge fields always use a start and an end marker. The markers use internally the TWPTextObj class. The start marker stores the name of the field in the 'name' property. The ObjType property of both, the start and the end marker is both set to wpobjMergeField.
By default this objects are displayed like in this image:
The display of the markers is optional. Please set the property WPRichText.InsertPointTextAttr.Hidden = true to hide the field markers. WPRichText.InsertPointTextAttr.Hidden should be set to true when the document is printed. To permanently delete the fields (and keep the text) use the procedure.DeleteFields.
This method will toggle display depending on the state of a check box:
procedure TForm1.ShowFieldsClick(Sender: TObject);
begin
if ShowFields.Checked then
begin
wprichtext1.InsertPointAttr.hidden:=false;
wprichtext1.automatictextattr.BackgroundColor := clYellow;
wprichtext1.automatictextattr.UseBackgroundColor := TRUE;
end else
begin
wprichtext1.InsertPointAttr.hidden:=true;
wprichtext1.automatictextattr.UseBackgroundColor := FALSE;
end;
wprichtext1.ReformatAll(false, true);
end;
It is also possible to show a different text in a different color.
The text is defined by the public (not published) properties CodeOpeningText and CodeClosingText. The variables %N, %S, %Y and %P can be used. The color can be changed with property CodeTextColor.
WPRichText1.InsertPointAttr.CodeOpeningText := '[%N=';
// %N inserts the TWPTextObj.Name property
// %S inserts the TWPTextObj.Source property
// %Y inserts the TWPTextObj.StyleName property - only useful for span styles
// %P inserts the TWPTextObj.Params property
WPRichText1.InsertPointAttr.CodeClosingText := ']';
WPRichText1.InsertPointAttr.CodeTextColor := clBlue;
This is how the field is displayed now:
Display Fieldnames only:
WPTools 8 also allows to hide the embedded text and the fieldmarker and display just a small box with the field marker:
This mode is controlled by property ShowMergeFieldNames. The value TRUE will show the boxes while the value FALSE will enabled the default display (with the field markers visible or not).
You can also replace the current field contents with the names of the fields with a OnDoMailMergGetText handler as simple as
procedure TForm1.DoMailMergGetText(
Sender: TObject; const inspname: string;
Contents: TWPMMInsertTextContents);
begin
Contents.StringValue := inspname;
end;
This works because mail merge does not destroy the fields, it just replaces the contents of the fields.