Results 1 to 3 of 3

Thread: How can I convert the content of a ritchTextBox to HTML?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    How can I convert the content of a ritchTextBox to HTML?

    Is it possible? let's say the text is colorized in my richTextBox, and it has bold, italic and all those kind of stuff in it. How can I convert it to HTML or, lets ms-word's file?

  2. #2
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    It would be a matter of going through all the text, and replacing certain properties with HTML tags. Say you found a line of text that was bold, you would insert <B> and </B> tags around it.

    It would take a little time to write the loops for it, but it shouldn't be too hard.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    I got this original routine from some where and I don't remember. I made so many changes to it that it is an almost completely different routine now, but I should credit some anonymous author who wrote the initial code but I don't know who it is. Anyway here it is...

    I use it for email so the subject becomes the title of the page. You call like this. The LockWindowUpdate call prevents you from seeing the cursor scroll.

    VB Code:
    1. Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    2.  
    3. Call LockWindowUpdate(RichText1.hWnd)
    4. sTemp = RichToHTML(RichText1, sSubject, 0, Len(RichText1.Text))
    5. Call LockWindowUpdate(0)


    VB Code:
    1. Public Function RichToHTML(rtbRichTextBox As RichTextLib.RichTextBox, sSubject As String, lngStartPosition As Long, lngEndPosition As Long) As String
    2.    
    3.     Dim blnBold As Boolean, blnUnderline As Boolean, blnStrikeThru As Boolean
    4.     Dim blnItalic As Boolean, strLastFont As String, lngLastFontColor As Long
    5.     Dim lngBlue As Long, lngCurText As Long, strHex As String, intLastAlignment As Integer
    6.     Dim bInBullet As Boolean, strHTML As String
    7.    
    8.     Const AlignLeft = 0, AlignRight = 1, AlignCenter = 2
    9.            
    10.     lngLastFontColor& = -1 'no color
    11.    
    12.     strHTML = "<div>"
    13.    
    14.     For lngCurText& = lngStartPosition& To lngEndPosition&
    15.         rtbRichTextBox.SelStart = lngCurText&
    16.         rtbRichTextBox.SelLength = 1
    17.        
    18.  
    19.         If (Not bInBullet) And (Len(rtbRichTextBox.SelText) = 0) Or (intLastAlignment <> rtbRichTextBox.SelAlignment) Then
    20.             intLastAlignment = rtbRichTextBox.SelAlignment
    21.             Select Case rtbRichTextBox.SelAlignment
    22.                Case AlignLeft: strHTML = strHTML & "</div><div align=left>"
    23.                Case AlignRight: strHTML = strHTML & "</div><div align=right>"
    24.                Case AlignCenter: strHTML = strHTML & "</div><div align=center>"
    25.                Case Else: strHTML = strHTML & "</div><div align=left>"
    26.             End Select
    27.            
    28.         End If
    29.        
    30.         If blnBold <> rtbRichTextBox.SelBold Then
    31.             If rtbRichTextBox.SelBold = True Then
    32.                 strHTML$ = strHTML$ & "<b>"
    33.             Else
    34.                 strHTML$ = strHTML$ & "</b>"
    35.             End If
    36.             blnBold = rtbRichTextBox.SelBold
    37.         End If
    38.  
    39.         If blnUnderline <> rtbRichTextBox.SelUnderline Then
    40.             If rtbRichTextBox.SelUnderline = True Then
    41.               strHTML = strHTML & "<u>"
    42.             Else
    43.               strHTML = strHTML & "</u>"
    44.             End If
    45.             blnUnderline = rtbRichTextBox.SelUnderline
    46.         End If
    47.        
    48.         If blnItalic <> rtbRichTextBox.SelItalic Then
    49.             If rtbRichTextBox.SelItalic = True Then
    50.               strHTML = strHTML & "<i>"
    51.             Else
    52.               strHTML = strHTML & "</i>"
    53.             End If
    54.             blnItalic = rtbRichTextBox.SelItalic
    55.         End If
    56.        
    57.         If blnStrikeThru <> rtbRichTextBox.SelStrikeThru Then
    58.             If rtbRichTextBox.SelStrikeThru = True Then
    59.                 strHTML = strHTML & "<s>"
    60.             Else
    61.                 strHTML = strHTML & "</s>"
    62.             End If
    63.             blnStrikeThru = rtbRichTextBox.SelStrikeThru
    64.         End If
    65.        
    66.         If strLastFont$ <> rtbRichTextBox.SelFontName Then
    67.             strLastFont$ = rtbRichTextBox.SelFontName
    68.             strHTML = strHTML & "<font face=""" & strLastFont$ & """>"
    69.         End If
    70.        
    71.         If lngLastFontColor& <> rtbRichTextBox.SelColor Then
    72.             lngLastFontColor& = rtbRichTextBox.SelColor
    73.            
    74.             ''Get hexidecimal value of color
    75.             strHex$ = Hex$(rtbRichTextBox.SelColor)
    76.             strHex$ = String$(6 - Len(strHex$), "0") & strHex$
    77.             strHex$ = Right$(strHex$, 2) & Mid$(strHex$, 3, 2) & Left$(strHex$, 2)
    78.            
    79.             strHTML = strHTML & "<font color=#" & strHex$ & ">"
    80.         End If
    81.        
    82.                
    83.         If bInBullet And (Len(rtbRichTextBox.SelText) = 0) Then
    84.             strHTML = strHTML & "</li><li>"
    85.         End If
    86.        
    87.         If Right$(strHTML, 13) = "<li></li><li>" Then strHTML = Left$(strHTML, Len(strHTML) - 9)
    88.        
    89.         If bInBullet <> rtbRichTextBox.SelBullet Then
    90.             If rtbRichTextBox.SelBullet = True Then
    91.                 strHTML = strHTML & "<ul><li>"
    92.             Else
    93.                 If Right$(strHTML, 9) = "</li><li>" Then strHTML = Left$(strHTML, Len(strHTML) - 9)
    94.                 strHTML = strHTML & "</li></ul>"
    95.             End If
    96.             bInBullet = rtbRichTextBox.SelBullet
    97.         End If
    98.        
    99.         strHTML = strHTML + rtbRichTextBox.SelText
    100.    
    101.     Next
    102.    
    103.     If Right$(strHTML, 14) = "<div align=left>" Then strHTML = Left$(strHTML, Len(strHTML) - 16)
    104.     If Right$(strHTML, 15) = "<div align=right>" Then strHTML = Left$(strHTML, Len(strHTML) - 17)
    105.     If Right$(strHTML, 16) = "<div align=center>" Then strHTML = Left$(strHTML, Len(strHTML) - 18)
    106.     If Left$(strHTML, 4) = "</div>" Then strHTML = Right$(strHTML, Len(strHTML) - 4)
    107.    
    108.     strHTML = Replace(strHTML, "<div></div>", "<br>")
    109.     strHTML = Replace(strHTML, "<div align=left></div><div align=left></div>", "<br>")
    110.     strHTML = Replace(strHTML, "<div align=right></div><div align=right></div>", "<br>")
    111.     strHTML = Replace(strHTML, "<div align=center></div><div align=center></div>", "<br>")
    112.    
    113.     If Right$(strHTML, 4) = "<li>" Then strHTML = Left$(strHTML, Len(strHTML) - 4) & "</ul></div>"
    114.    
    115.     strHTML = "<HTML><HEAD><TITLE>" & sSubject & "</TITLE></HEAD><BODY>" & strHTML & "</BODY></HTML>"
    116.     RichToHTML = strHTML
    117.  
    118. End Function
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

Posting Permissions

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



Click Here to Expand Forum to Full Width