Link to home
Start Free TrialLog in
Avatar of shawn857
shawn857

asked on

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
Avatar of jimyX
jimyX

>   "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.
Avatar of shawn857

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using clipboard is not elegant solution ... use stream instead.
Article on About.com tells more about appending text.
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.
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
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,
> 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