RichEdit replace string without lost.
Hello all,
I want replace string in RichEdit before print with FastReport. I load file to RichEdit object, but when replace string lost a format of document.
Please see source below!
Can I fix this?
Thanks.
var
RichView: TfrxRichView;
begin
Form229.frxReport1.Clear;
Form229.frxReport1.LoadFromFile( 'spr0059.fr3' );
RichView := TfrxRichView( Form229.frxReport1.FindObject( 'Rich1' ) );
If RichView <> Nil Then Begin
RichView.RichEdit.Lines.LoadFromFile( 'doc.rtf' );
RichView.RichEdit.Lines.Text := RepStr( RichView.RichEdit.Lines.Text,'$rival_docnom$','123' );
// when replace string lost text formating of RichEdit and FastReport show it at PlanText !!! Any Ideas!
RichView.RichEdit.SelStart := 0;
RichView.RichEdit.SelLength := Length( RichView.RichEdit.Text );
RichView.RichEdit.SelAttributes.Name := Form3.aSystem[33];
RichView.RichEdit.Font.Size := 10;
End;
Form229.frxReport1.ShowReport( True );
end;
function RepStr( cStr,cRemove,cReplace: String ) : String;
var
cNew: String;
begin
Result := '';
cNew := cStr;
Repeat
cNew := StuffString( cNew,Pos( cRemove,cNew ),Length( cRemove ),cReplace );
Until Pos( cRemove,cNew ) = 0;
Result := cNew;
end;
I want replace string in RichEdit before print with FastReport. I load file to RichEdit object, but when replace string lost a format of document.
Please see source below!
Can I fix this?
Thanks.
var
RichView: TfrxRichView;
begin
Form229.frxReport1.Clear;
Form229.frxReport1.LoadFromFile( 'spr0059.fr3' );
RichView := TfrxRichView( Form229.frxReport1.FindObject( 'Rich1' ) );
If RichView <> Nil Then Begin
RichView.RichEdit.Lines.LoadFromFile( 'doc.rtf' );
RichView.RichEdit.Lines.Text := RepStr( RichView.RichEdit.Lines.Text,'$rival_docnom$','123' );
// when replace string lost text formating of RichEdit and FastReport show it at PlanText !!! Any Ideas!
RichView.RichEdit.SelStart := 0;
RichView.RichEdit.SelLength := Length( RichView.RichEdit.Text );
RichView.RichEdit.SelAttributes.Name := Form3.aSystem[33];
RichView.RichEdit.Font.Size := 10;
End;
Form229.frxReport1.ShowReport( True );
end;
function RepStr( cStr,cRemove,cReplace: String ) : String;
var
cNew: String;
begin
Result := '';
cNew := cStr;
Repeat
cNew := StuffString( cNew,Pos( cRemove,cNew ),Length( cRemove ),cReplace );
Until Pos( cRemove,cNew ) = 0;
Result := cNew;
end;
Comments
instead of RichView.RichEdit.Lines.Text := RepStr( RichView.RichEdit.Lines.Text,'$rival_docnom$','123' );
Give me a sample.
When use .Add() addet lines in RichEdit is a plantext not in Rich format. When use Loadfromfile() all is ok.
Thanks.
RichView.RichEdit.Lines.LoadFromFile( ExtractFilePath( Application.ExeName ) + 'doc.rtf' );
SearchAndReplace( RichView.RichEdit,'$rival_docnom$','123' );
SearchAndReplace( RichView.RichEdit,'$rival_docdate$','11.01.2005' );
procedure SearchAndReplace( Rich: TrxRichEdit; InSearch,InReplace: String );
var
X,ToEnd: Integer;
begin
X := 0;
ToEnd := Length( Rich.Text );
X := Rich.FindText( inSearch,X,ToEnd,[] );
While X <> -1 Do Begin
Rich.SelStart := X;
Rich.SelLength := Length( inSearch );
Rich.SelText := inReplace;
X := Rich.FindText( inSearch,X + Length( inReplace ),ToEnd,[] );
End;
end;