4

Is there a way to get the RTF data from a richedit without using savetostream as in

strStream := TStringStream.Create('') ;
try
  RichEdit.Lines.SaveToStream(strStream);
  Text := strStream.DataString;
  strStream.CleanupInstance;
finally
  strStream.Free
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
Tim
  • 1,549
  • 1
  • 20
  • 37
  • 2
    Do not call `CleanupInstance` explicitly, it is called while the stream is being destroyed. – Sertac Akyuz Sep 03 '10 at 18:19
  • In other words, use `Free()` instead of `CleanupInstance()`. And you should use a `try/finally` in case `SaveToStream()` raises an exception. – Remy Lebeau Aug 07 '12 at 20:49

2 Answers2

5

Tim the only way to get the RTF data from an RichEdit control is using a Stream because the windows message (EM_STREAMOUT) wich retrieve the RTF Data require a EditStreamCallback structure, this is the way used by windows to transfer rtf data into or out of a richedit control.

So you can use your own sample code, or implement the call to the windows message EM_STREAMOUT.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
3
function RichTextToStr(red : TRichEdit) : string;

var   ss : TStringStream;

begin
  ss := TStringStream.Create('');

  try
    red.Lines.SaveToStream(ss);
    Result := ss.DataString;
  finally
    ss.Free;
  end;
end;

procedure CopyRTF(redFrom,redTo : TRichEdit);

var   s : TMemoryStream;

begin
  s := TMemoryStream.Create;

  try
    redFrom.Lines.SaveToStream(s);
    s.Position := 0;
    redTo.Lines.LoadFromStream(s);
  finally
    s.Free;
  end;
end;

I can attest deviation from the pattern results in frustration....

takrl
  • 6,356
  • 3
  • 60
  • 69
user463032
  • 31
  • 1