Solved

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

Posted on 2015-02-08
8
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 27

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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