Saving a text file looses CR


I’m using a TextArea for the user to enter an Email message that will be used multiple times.

The proble, is in the finished email (Client is Mac Mail) the carriage returns are lost. It comes out as 1 long string. However they show in the TextArea control?

I save it with this code:

  Dim f as FolderItem
  Dim t as TextOutputStream
  msEmailBody = txtBodyOfEmail.Text
  If f <> Nil then
    mbNedToSaveBody = False
    me.Enabled = False
  End if

And Loaded with this code:

  Dim f as FolderItem
  Dim t As TextInputStream
  If f <> Nil and f.Exists then
    't.Encoding=Encodings.MacRoman //specify encoding of input stream
    msEmailBody = txtBodyOfEmail.Text
    mbNedToSaveBody = False
    btnSave.Enabled = False

I’m sure it’s something simple, but I can’t figure it out.


I’m not sure exactly how EndOfLine characters should be handled across different platforms but the following workaround should do the trick:

In your save method, replace:

msEmailBody = txtBodyOfEmail.Text


msEmailBody = ReplaceAll(txtBodyOfEmail.Text, Chr(13), EndOfLine)

From what I could gather in the debugger, the textarea stores “end of lines” as a single ASCII 13 character. On some systems the ASCII 13 need to be followed by a ASCII 10 character. The replace instruction above does just that (inserts ASCII 10 characters after all the character 13 characters).

An OSX text ends with LF. If you need to save some text to be used in an email, that was encoded as MacRoman first, converting it to UTF8 before sending out is advised. As for converting the EndOfLines from LF to CR/LF, try ReplaceLineEndings (SourceString, EndOfLine.Windows)

Thanks Ricardo, there I also learning something new. ReplaceLineEndings is a much more elegant way to replace line endings.

Ricardo thanks, I’ll try it.

How do I convert it or make sure it is UTF8?

Check this: :wink:

Alwin, you are 100 years ahead me in Xojo. I only knew this because I am forced to learn Xojo from the 2013 edition. So, sometimes I see some novelty. :wink:

The msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows) Did not work.

I read up on the converting.

Can you share a sample of the problem? Including that text file you are loading? You can zip and let’s test this “Attach file” feature I see below.

What attach feature?

…I see below when composing the message. Hey! The edition feature vanished.

Test Attaching. Forget it. It does not work. Use a service ike this


When I use this service, in the field email, I just put “@”.

[quote=9611:@Richard Albrecht]The msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows) Did not work.

I read up on the converting.[/quote]

Are you using a Mac Richard? I think you then you might have to use…

msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Macintosh)

I just tried :

msEmailBody = ReplaceLineEndings( txtBodyOfEmail.Text, EndOfLine.Windows).ConvertEncoding(Encodings.UTF8)

and that didn’t work.

Yes I’m using a Mac

Isn’t using EndOfLine.Macintosh converting it to Mac???

Let me try.

I think it then converts it to proper line endings for the Mac.

The Problem reported by Richard was missing CR’s. He told that his text had just LF’s. As OSX does. To save a text with CR+LF, we need to replace the LF’s by CR+LF’s. We we are confused, the bast way to know is a sample. :wink: