Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-08
8
Medium Priority
?
935 Views
Last Modified: 2015-02-09
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
Comment
Question by:shawn857
  • 4
  • 3
8 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 40597804
>   "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
 

Author Comment

by:shawn857
ID: 40597823
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
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 40597990
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 40598079
Using clipboard is not elegant solution ... use stream instead.
Article on About.com tells more about appending text.
0
 

Author Comment

by:shawn857
ID: 40599645
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
 

Author Comment

by:shawn857
ID: 40599670
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
 

Author Comment

by:shawn857
ID: 40599926
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
 
LVL 24

Expert Comment

by:jimyX
ID: 40600184
> 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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question