I have a RTF textbox that contains multiline text. I would like to extract the text only from this box to put in an e-mail.

I know that RTFBox.Text will return the plain text but it seems that it strips out the CRLF's as well and I need to keep those for the e-mail.

Thanks in advance for any help.

Hi,

I think you need to get the actual RTF back then parse the rich text and strip out any RTF coding and replace the RTF new Line character with vbcrlf.

It's been a while since I looked at RTF text so I'm not 100% sure of the syntax. Here is an example though of when I cleaned up Word generated "HTML"

Function CleanHTML (byref Input as String) AS String
Dim bInTag as boolean = false
dim sChar, Tagcode, Output as string
dim i as integer


for i =1 to len(Input)
        schar = mid(Input,i,1)
        if not bInTag then
            if schar ="<" then
                binTag =true
            else
                Output += schar
            end if
        else
            if schar = ">" then
                bInTag = False
                if Instr(TagCode, "</") <> 0 then
                    Output += TagCode
                elseif Instr(TagCode, <B) <> 0 Then
                    Output += "<b>"
                    'etc.
                end if
                TagCode=""
            else
                Tagcode += UCASE(schar)
            end if
        End if

next

As I said that's roughly what I did for HTML but I'm sure you could do somethng similar for RTF.

Textboxes use only the LF character. This is one way to handle it.

  Dim lines() As String = RichTextBox1.Lines
  'initialize stringbuilder to textlength + num lines  (this adds space for VbCr)
  Dim sb As New System.Text.StringBuilder(RichTextBox1.TextLength + lines.Length + 20)
  For Each l As String In lines
     sb.AppendLine(l) 'add line with CrLF appended
  Next
  Dim EmailMessage As String = sb.ToString

Edited 4 Years Ago by TnTinMN: mod code

Wouldn't it also work if you just replaced lf with crlf as in

RichTextBox1.Text.Replace(vbLf,vbCrLf)

Edited 4 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.