Strip rich text

Pummel
Pummel used Ask the Experts™
on
I need to take a rich text string and strip it of its formatting characters.  What function or procedure does this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This example writes to a textfile.. although you don't need to if you don't want.



var Txt:Ansistring;
    var F:textfile;
begin
 Richedit1.lines.LoadFromFile('Z:\myfile.rtf');
 Txt:=RichEdit1.Text;
 assignfile(f,'Z:\Output.txt');
 Rewrite(f);
 Write(f,txt);
 Closefile(f);
end;


Good luck!!

Author

Commented:
Well, I was wondering if I could do it without a descendent of TComponent.  I'd like to just call the function I'm looking for inside a small bpl or dll.  There's got to be a Windows function for this, isn't there?
Commented:
Hmmm... not that I am aware of. You see the RTF standard is a dynamic one. It is prone to change much more freqently than Windows' versions. You *could* theoretically open an RTF file and pull off all of the text that isn't an embedded code, but I wouldn't know where you would begin, especially considering that RTF codes can be nested. I'm sure that somewhere is this great big world someone has done this, but not me....I'll stick to the VCL. <g>

Good luck!!
 
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

I would check if TRichText can do it by selecting the text, putting it to the clipboard and see if it also creates a stripped text version.

Commented:
Hi

It's simple, but I'll be elaborate anyway:

The .Lines property has a function  GetText that does exactly what you want. Demonstration code with 1 RTF control, one Memo control and one button to execute the copy. The code to get the text and show it in the Memo1 control is trivial, attach this code to Button1:

procedure TForm1.Button1Click(Sender: TObject);
  var
  Temp:String;
begin
 Temp :=  RichEdit1.Lines.GetText;
 Memo1.Lines.Add(Temp);
end;

To see it work, paste some nice formatted text into the RichEdit1, then press Button1 to see it reappear as just letters in Memo1.

Commented:
Hmmm... that DID work:

  Richedit1.Selstart:=0;
  Richedit1.sellength:=length(RichEdit1.text);
  Clipboard.AsText:=RichEdit1.SelText;


Good luck!!

Commented:
You like that better than this:

Clipboard.AsText := RichEdit1.Lines.GetText;


Have fun!

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