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.

Recommended Answers

All 3 Replies

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

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

RichTextBox1.Text.Replace(vbLf,vbCrLf)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.