In a TWPRichText component you can edit a mail merge field, however this only edits the "displayed name" not the hidden field name.
I can use the FieldGetNames method to enumerate the field names but can't find a method to edit any of these names.
suggested FieldAtCP function works well, however - the help file describes this as Field at Cursor Position whereas I think it should be Field at Caret Position.
I use
var
obj: TWPTextObj;
begin
obj := wpRichText.FieldAtCP;
if assigned(obj) then
obj.Name := obj.EmbeddedText;
end;
in the onChange eventso that the field name will change automatically when the displayed name is updated.
Is this the best place to put the code?
Will this code cause other problems - where else is the TWPTextObject used?
To automatically check and update fields in code, how do I enumerate the field objects? I can use the FieldGetNames function to enumerate the names and I can use the FieldReadText(const fieldname: string):string function to get the embedded text but I think I need a FieldReadObj(const fieldname: string): TWPTextObjectfunction to get the object so that I can update its name.
thanks for the suggestion but in the documentation "cursor" is almost always used for "current insertion point" (unless "mouse cursor" is used). I know the word "caret" but that is used only for the blinking rectangle which marks the insertion point. "cursor" in the manual is more general (also works when caret is not visible etc)
You code is ok but is triggered too often in OnChange. I would use a PostMessage to execute that asynchronly in idle phases. FieldAtCP can be slow, esspecially if there is NO field!
Or, better, only excecute code like this for all fields when you need the names to be updated. You can use MergeText to update all fields!
FieldAtCP only checks wpobjMergeField so it should be ok. But if somebody enters a lot of text the name can be corrupted.
FieldGetNames is mainly there for compatibility to Version 4. If you use FieldGetListyou can fill a TWPTextObjList with object references - ready to be used with youre code.
The problem with the suggested FieldReadObj is that one field name can be used multiple times in a document. FieldGetList also accepts a name parameter and can so create a list of field objects starting with a certain name.
BTW.: As lower level function there is also the method CodeListTags.
the FieldGetList function is perfect - this function is not yet in the mail merge section of the help file - I should have looked into TWPRichText methods
#5♥ 0
This category is archived. No new posts are possible.