Solved

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

Posted on 2015-02-08
8
386 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 500 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
 
LVL 25

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now