Work with selections

Top  Previous  Next

In WPTools Version 9 the selection and cursor is always moved o the current field. So it is also possible to modify the text at the cursor position directly in the OnMailMergeGetText event. This was strictly forbidden in WPTools 4, since WPTools V5 it can be used carefully:

 

Insert a picture:

 img := TWPOImage.Create(WPRichText1);

 img.LoadFromFile(picname);

 WPRichText1.TextObjects.Insert(img)

 

Insert a table:

 WPRichText1.ClearSelection(true);

 WPRichText1.AddTable(2,2,true);

 

Insert formatted text

// Delete the current contents of the field

 WPRichText1.ClearSelection(true);

// Get the field default character attributes in 'WritingMode'

 WPRichText1.WritingAttr.CharAttr :=  Contents.MergeAttr.CharAttr;

// And add text with variations of the attributes

 WPRichText1.WritingAttr.IncludeStyle(afsUnderline);

 WPRichText1.WritingAttr.IncludeStyle(afsBold);

 WPRichText1.InputString('81541');

 WPRichText1.WritingAttr.ExcludeStyle(afsUnderline);

 WPRichText1.InputString(' Munich');

 

Please note that fields are represented by start and end objects (cass TWPTextObj). This objects have several useful properties, including 'EmbeddedText' which can be used to read and write the text between the markers. So if you need to quickly update the field at the cursor position you can set FiedlAtCP.EmbeddedText := 'NewText';

 

The references to the field obejcts are available in the OnMailMergeGetText procedure:

Contents.StartInspObject and Contents.EndInspObject. Please do not mix up with CurrentTextObject, that is the reference of the first object inside the field. CurrentTxtObject is the reference of the first image object, that is the same as CurrentTextObject.ObjRef.