I use bookmarks for storing information which may appear in a popup menu. Some text is selected and the bookmark name inserted.
Sometimes the user will select text which already contains a bookmark somewhere. The old bookmark must then be deleted before a new one is inserted.
Essentially I use the following code:
Select text and click an Insert button.
DeleteBookmarkInSelection(WPRichText1);WPRichText1.BookmarkInput('TEST');
The procedureDeleteBookmarkInSelection is:
procedure DeleteBookmarkInSelection( WPRichText : TWPRichText);
var
TxtObj : TWPTextObj;
const S = 'TOBEDELETED';
begin
with WPRichText do
begin
CPPosition := SelStart;
repeat
TxtObj := BookmarkAtCP;
CPMoveNext;
until (TxtObj <> nil) or (CPPosition = SelStart + SelLength);
if TxtObj <> nil then
begin
TxtObj.Name := S; //Rename so other bookmarks with same name are not deleted!
BookmarkDelete(S,false,false);
end;
end;
end;
Alles anzeigen
There is a slight problem: After the bookmark deletion the selection is shifted one character to the right so the bookmark starts at the second character of the first selected word.
Is there an obscure bug in my code or is there a problem with BookmarkDelete?
Julian: I have sent you a demo which shows the effect.