Preserve Attributes of text inside a field

<< Click to Display Table of Contents >>

Navigation:  Programming > Mail Merge (replace fields with data) and data forms > Update Field (Insert Text from Database) > Event OnMailMergeGetText >

Preserve Attributes of text inside a field

Normally, while merging in ANSI text (in contrast to RTF text), the inserted text will get the attributes of the first field marker.

 

If the text inside the field markers is specially formatted, this format properties are usually lost.

 

But it is possible to force the inserted text to use certain attributes. This is done with property MergeAttr.

 

So, just a few lines can be used to read the character attributes of the first character in the field:

 

Contents.MergeAttr.CharAttr :=

     Contents.StartInspObject.ParentPar.CharAttr

     [ Contents.StartInspObject.ParentPosInPar + 1 ];

 

Here we read the CharAttr index of the character after the field marker "StartInspObject".

 

This code has the problem that it does not check, if the firld marker happen to be the last character in a paragraph. This is not usual, but it can happen.

 

So better take advantage from the fact, that the cursor is before the first character in the field during OnMailMergeGetText.

 

This code will work to read the first character attributes:

 

Contents.MergeAttr.CharAttr := WPRichText1.CPCharAttr;