Move RTF text to a string (Delphi 2009)

LeTay
LeTay used Ask the Experts™
on
I used the following code in Delphi 2006 to move the text (with formatting stuff) of a TRichEdit component to a string variable :
function RTF2String(RTF:TRichEdit):string;
var
 MS:TMemoryStream;
begin
 RTF.PlainText := False;
 MS := TMemoryStream.Create;
 RTF.Lines.SaveToStream(MS);
 SetLength(Result,MS.Size);
 Move(MS.Memory^,Pointer(Result)^,Length(Result));
end;
This was perfect with Delphi 2006, but now it does not work correctly with Delphi 2009 due to Unicode
What shall I code ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
can you try with

function RTF2String(RTF:TRichEdit):AnsiString;
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
or better yet

function MemoryStreamToString(M: TMemoryStream): string;
begin
  SetString(Result, M.Memory, M.Size div SizeOf(Char));
end;

function RTF2String(RTF:TRichEdit):string;
var
 MS:TMemoryStream;
begin
 RTF.PlainText := False;
 MS := TMemoryStream.Create;
 RTF.Lines.SaveToStream(MS);
 Result:=MemoryStreamToString(MS);
 MS.Free;
end;

Open in new window

Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
this last one should even work with old delphi version
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
Compiler fails on the SetString function : [DCC Erreur] U01.pas(928): E2008 Types incompatibles
This is on M.Memory
Emmanuel PASQUIERFreelance Project Manager
Top Expert 2010

Commented:
SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char));

Author

Commented:
On the other hand, your first solution works (declare the function as ANSIString) but it is not optimal
I prefer a solution like the second one, but which compiles

Author

Commented:
Corrected
Now the compilation is okay
But the result is still bad (as the original)
Freelance Project Manager
Top Expert 2010
Commented:
Yeah, I figured it would be with your last comment. If first solution is OK then it means that saving in a the rich text in a TMemoryStream is done in ASCII characters, not unicode. I was not sure as I don't have D2009.
so the first solution is the one you should take, as it cannot be done any better (except the MS.Free; I added at the end of procedure in the second solution)

You can even try with SetString (but the AnsiString version) instead of SetLength+Move, it should be equivalent

so :
function RTF2String(RTF:TRichEdit):AnsiString;
var
 MS:TMemoryStream;
begin
 RTF.PlainText := False;
 MS := TMemoryStream.Create;
 RTF.Lines.SaveToStream(MS);
 SetString(Result, M.Memory, M.Size);
// SetLength(Result,MS.Size);
// Move(MS.Memory^,Pointer(Result)^,Length(Result));
 MS.Free;
end;

Open in new window

Author

Commented:
Many thanks
I forgot then MS.Free in the code, but my memory manager wouldl tell me (FastMM4)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial