Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1165
  • Last Modified:

How to write contents of a TRichEdit to a file...

Hi, I'm pretty inexperienced using the TRichEdit control and I'm wondering how I can output the colored contents of it to a text file, then when the text file is viewed, it will show the characters in their proper colors. I suppose the TRichEdit contents must be written to a .rtf file, I'm assuming... and then this file viewed in something like Wordpad?

Thanks!
    Shawn
0
shawn857
Asked:
shawn857
  • 4
  • 3
1 Solution
 
jimyXCommented:
>   "I suppose the TRichEdit contents must be written to a .rtf file, I'm assuming... and then this file viewed in something like Wordpad?"
Exactly.

You can use Lines.SaveToFile:
  RichEdit1.Lines.SaveToFile('D:\YourFileName.rtf');

Open in new window


Edit:
Not only ".rtf", you can use ".doc" and open with MS Word or Libre Office.
0
 
shawn857Author Commented:
Thanks Jimy... how do I use this "SaveToFile" method in conjunction with normal "writeln"'s to the same output file? In other words, I already have my output file open (declared as type "text") and writing out a report to it - mostly just normal strings and calculation results. About half-way down in this report is where I want to write out the contents of my TRichEdit... this is the only portion of this output file that will have color-coded text. I just tried a small test now, where I had my text file already open and wrote out some normal text to it, then I inserted your suggestion right after a normal writeln statement:

RichEdit1.Lines.SaveToFile('C:MyReport.rtf');

This causes a runtime error on that line:

'Cannot create file "C:MyReport.rtf". The process cannot access the file because it is being used by another process.'

Do I just need to close my output file right before the SaveToFile command... perform the SaveToFile (will it append to the already existing file, or overwrite it?), then re-open my output file and continue to append the remainder of my regular text?

Thanks!
    Shawn
0
 
jimyXCommented:
SaveToFile overwrites without warning.

Best practice is to accumulate all your data in one place and write to disk once.

In your case, you will not be able to start your rtf file as a normal text file, and then add formatted text.
The moment you save plain text to the file it will not be recognized as RTF file.

That is a challenge, it is difficult enough to copy from one RichEdit to another without losing the formatted text, forget about with another text, to a text file.

So what you can do is use that RichEdite to load all your report from the beginning, and save one time to the file, after all is done.
If not possible, then you have two options, the easy and the hard way.
The easy way, using the Clipboard:
procedure TForm1.Button1Click(Sender: TObject);
var
  RE: TRichEdit;
  str:String;
begin
  // Just add some text simulating your report data
  str:= 'ABC'+#13;
  Str:= Str+ 'XYz';

  //create another RichEdit for the sake of maintaining the rtf text
  RE:= TRichEdit.Create(nil);
  RE.Parent:= self;
  Re.Visible:= False;
  RE.Lines.Add(Str);
  RE.Lines.Add(#13);
  RE.SelectAll;
  RE.CopyToClipboard;    // move text to clipboard
  RE.Free;

  RichEdit1.Lines.BeginUpdate;
  RichEdit1.SelStart:= 0;
  RichEdit1.PasteFromClipboard;
  RichEdit1.Lines.SaveToFile('D:\j.rtf');
  while RichEdit1.Lines.Count > 1 do       // make sure to delete the inserted lines
    RichEdit1.Lines.Delete(0);
  RichEdit1.Lines.EndUpdate;
end;

Open in new window


The hard way:
Use Streams but at the end you need to parse the RTF and produce your own rtf file for the report.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Sinisa VukCommented:
Using clipboard is not elegant solution ... use stream instead.
Article on About.com tells more about appending text.
0
 
shawn857Author Commented:
Thanks Jimy, I like the idea of the "easy way" using clipboard... but I kinda don't follow your code. You have a "RichEdit1.PasteFromClipboard;" statement that I don't understand. The colored text I want to output is in the RichEdit1 control... won't pasting something from the clipboard INTO Richedit1 wipe out my colored text that's in there?

Basically I have only a small report file that I want to output to a file - about 15 lines of plain text, then 1 line of RichEdit colored text, followed by another 10 or 15 lines of plain text. The RichEdit colored text part is in the middle of my report.

Thanks
   Shawn

P.S: Thanks Sinisa, I looked at that article and code using Streams and I found it very difficult to follow how it worked.
0
 
shawn857Author Commented:
OK Jimy, I get what it's doing now... sorry, please disregard my last email. Yeah, I think this will work fine! I'll be back in a bit...

Thanks
   Shawn
0
 
shawn857Author Commented:
Looking good Jimy, your clipboard trick works great for the RichEdit. What I did instead of "inserting" (using paste) my regular string text into the start of RichEdit1... I just created another invisible RichEdit item and wrote everything to that (RichEdit1 colored text included - using the clipboard trick once again), then using "SaveToFile", wrote out everything to my output file. Worked good.

Thanks!
   Shawn

P.S: And thank you also Sinisa for your contribution... JimyX's solution was just right for me however,
0
 
jimyXCommented:
> I just created another invisible RichEdit item and wrote everything to that (RichEdit1 colored text included - using the clipboard trick once again)
Sounds better, so not to mess in the line the user is editing.

Regards,
J
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now