Convert RichTextBox to HTML string-VBForums
Results 1 to 5 of 5

Thread: Convert RichTextBox to HTML string

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Convert RichTextBox to HTML string

    vb.net Code:
    1. Public Shared Function FromRtf(ByVal rtf As RichTextBox) As String
    2.         Dim b, i, u As Boolean
    3.         b = False : i = False : u = False
    4.         Dim fontfamily As String = "Arial"
    5.         Dim fontsize As Integer = 12
    6.         Dim htmlstr As String = String.Format("<html>{0}<body>{0}<div style=""text-align: left;""><span style=""font-family: Arial; font-size: 12pt;"">", vbCrLf)
    7.         Dim x As Integer = 0
    8.         While x < rtf.Text.Length
    9.             rtf.Select(x, 1)
    10.             If rtf.SelectionFont.Bold AndAlso (Not b) Then
    11.                 htmlstr &= "<b>"
    12.                 b = True
    13.             ElseIf (Not rtf.SelectionFont.Bold) AndAlso b Then
    14.                 htmlstr &= "</b>"
    15.                 b = False
    16.             End If
    17.             If rtf.SelectionFont.Italic AndAlso (Not i) Then
    18.                 htmlstr &= "<i>"
    19.                 i = True
    20.             ElseIf (Not rtf.SelectionFont.Italic) AndAlso i Then
    21.                 htmlstr &= "</i>"
    22.                 i = False
    23.             End If
    24.             If rtf.SelectionFont.Underline AndAlso (Not u) Then
    25.                 htmlstr &= "<u>"
    26.                 u = True
    27.             ElseIf (Not rtf.SelectionFont.Underline) AndAlso u Then
    28.                 htmlstr &= "</u>"
    29.                 u = False
    30.             End If
    31.             If fontfamily <> rtf.SelectionFont.FontFamily.Name Then
    32.                 htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", rtf.SelectionFont.FontFamily.Name, fontsize)
    33.                 fontfamily = rtf.SelectionFont.FontFamily.Name
    34.             End If
    35.             If fontsize <> rtf.SelectionFont.SizeInPoints Then
    36.                 htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", fontfamily, rtf.SelectionFont.SizeInPoints)
    37.                 fontsize = rtf.SelectionFont.SizeInPoints
    38.             End If
    39.             Dim curchar As String = rtf.SelectedText
    40.             Select Case curchar
    41.                 Case vbCr, vbLf : curchar = "<br />"
    42.                 Case "&" : curchar = "&amp;" : x += "&amp;".Length - 1
    43.                 Case "<" : curchar = "&lt;" : x += "&lt;".Length - 1
    44.                 Case ">" : curchar = "&gt;" : x += "&gt;".Length - 1
    45.                 Case " " : curchar = "&nbsp;" : x += "&nbsp;".Length - 1
    46.             End Select
    47.             rtf.SelectedText = curchar
    48.             x += 1
    49.         End While
    50.         Return htmlstr & String.Format("</span>{0}</body>{0}</html>", vbCrLf)
    51.     End Function
    Last edited by minitech; Apr 5th, 2010 at 06:56 PM.

  2. #2
    New Member
    Join Date
    Dec 2009
    Location
    Indonesia
    Posts
    4

    Re: Convert RichTextBox to HTML string

    Actually I was making something similiar to these. But your code seems more efficient than mine
    But I'll check if it's work the same.

  3. #3
    Lively Member Amerigo's Avatar
    Join Date
    Dec 2008
    Location
    PSR B1620-26 c-1
    Posts
    126

    Re: Convert RichTextBox to HTML string

    Hey there Minitech,
    That function works to convert, however, when sending in an email, it comes up with jibberish code using:
    Mail.Body = FromRtf(TextField) '(Textfield is a richtextbox control.)

    edit:
    If using Mail.body = TextField.text, then I get text without formating or images.
    If using Mail.Body = TextField.RTF then I get errors.
    Last edited by Amerigo; Jun 7th, 2014 at 03:40 PM.
    Anyone who does not wonder, is either omnipotent or a fool.
    Amerigoware <<<My Projects

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    29,167

    Re: Convert RichTextBox to HTML string

    Right... because you se the Body of the email rather than the HTMLBody... the email class contains both a body, which is for plain text and will be displayed if the client doesn't support html email... then there's also the HTMLBody property which you can set to display rich emails, if the client supports it (in the case it doesn't then it displays the Body).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Convert RichTextBox to HTML string

    Looking back on this: don’t use it. It shouldn’t work. I hope it doesn’t work; if it does, it does by magic. Try an RTF parser or something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.