In a mailmerge routine, I need to delete full lines of text if besides a special code the line contains only spaces and tab characters, so I use the following routine.
Code
var
iBegin, iFrom, iTo: Integer;
sCh: String;
sCod: String;
begin
sCod := 'P_E002G11S01';
iBegin := Pos(sCod, WPRtf1.Text);
// Go further until end of line
iTo := iBegin+1;
sCh := MidStr(WPRtf1.Text, iTo, 1);
while sCh <> #10 do
begin
sCh := MidStr(WPRtf1.Text, iTo, 1);
if (sCh = #13) or (sCh = #10) then break;
if (sCh <> ' ') and (sCh <> #9) then
begin
sCh := 'X';
break;
end;
iTo := iTo+1;
end;
// Other chars in the line, delete line is not possible
if sCh = 'X' then Exit;
// Delete the line, CrLf included
if iTo - iFrom > 1 then
WPRtf1.text := LeftStr(WPRtf1.text, iFrom-1) + MidStr(WPRtf1.text, iTo+2, 9999999999);
WPRtf1.ReformatAll(true, false);
end;
Alles anzeigen
The problem is that although WPRtf1.Text is working as expected, it seems that after this routine, the RTF formatting is lost when rendering on screen (text color, bold, underline, etc).
If I use WPRtf1.AsString, I can see all the RTF code plus my text, but that formatting is not rendered on screen, even if I am using ReformatAll to synchronize.