Public Function RichToHTML(rtbRichTextBox As RichTextLib.RichTextBox, sSubject As String, lngStartPosition As Long, lngEndPosition As Long) As String
Dim blnBold As Boolean, blnUnderline As Boolean, blnStrikeThru As Boolean
Dim blnItalic As Boolean, strLastFont As String, lngLastFontColor As Long
Dim lngBlue As Long, lngCurText As Long, strHex As String, intLastAlignment As Integer
Dim bInBullet As Boolean, strHTML As String
Const AlignLeft = 0, AlignRight = 1, AlignCenter = 2
lngLastFontColor& = -1 'no color
strHTML = "<div>"
For lngCurText& = lngStartPosition& To lngEndPosition&
rtbRichTextBox.SelStart = lngCurText&
rtbRichTextBox.SelLength = 1
If (Not bInBullet) And (Len(rtbRichTextBox.SelText) = 0) Or (intLastAlignment <> rtbRichTextBox.SelAlignment) Then
intLastAlignment = rtbRichTextBox.SelAlignment
Select Case rtbRichTextBox.SelAlignment
Case AlignLeft: strHTML = strHTML & "</div><div align=left>"
Case AlignRight: strHTML = strHTML & "</div><div align=right>"
Case AlignCenter: strHTML = strHTML & "</div><div align=center>"
Case Else: strHTML = strHTML & "</div><div align=left>"
End Select
End If
If blnBold <> rtbRichTextBox.SelBold Then
If rtbRichTextBox.SelBold = True Then
strHTML$ = strHTML$ & "<b>"
Else
strHTML$ = strHTML$ & "</b>"
End If
blnBold = rtbRichTextBox.SelBold
End If
If blnUnderline <> rtbRichTextBox.SelUnderline Then
If rtbRichTextBox.SelUnderline = True Then
strHTML = strHTML & "<u>"
Else
strHTML = strHTML & "</u>"
End If
blnUnderline = rtbRichTextBox.SelUnderline
End If
If blnItalic <> rtbRichTextBox.SelItalic Then
If rtbRichTextBox.SelItalic = True Then
strHTML = strHTML & "<i>"
Else
strHTML = strHTML & "</i>"
End If
blnItalic = rtbRichTextBox.SelItalic
End If
If blnStrikeThru <> rtbRichTextBox.SelStrikeThru Then
If rtbRichTextBox.SelStrikeThru = True Then
strHTML = strHTML & "<s>"
Else
strHTML = strHTML & "</s>"
End If
blnStrikeThru = rtbRichTextBox.SelStrikeThru
End If
If strLastFont$ <> rtbRichTextBox.SelFontName Then
strLastFont$ = rtbRichTextBox.SelFontName
strHTML = strHTML & "<font face=""" & strLastFont$ & """>"
End If
If lngLastFontColor& <> rtbRichTextBox.SelColor Then
lngLastFontColor& = rtbRichTextBox.SelColor
''Get hexidecimal value of color
strHex$ = Hex$(rtbRichTextBox.SelColor)
strHex$ = String$(6 - Len(strHex$), "0") & strHex$
strHex$ = Right$(strHex$, 2) & Mid$(strHex$, 3, 2) & Left$(strHex$, 2)
strHTML = strHTML & "<font color=#" & strHex$ & ">"
End If
If bInBullet And (Len(rtbRichTextBox.SelText) = 0) Then
strHTML = strHTML & "</li><li>"
End If
If Right$(strHTML, 13) = "<li></li><li>" Then strHTML = Left$(strHTML, Len(strHTML) - 9)
If bInBullet <> rtbRichTextBox.SelBullet Then
If rtbRichTextBox.SelBullet = True Then
strHTML = strHTML & "<ul><li>"
Else
If Right$(strHTML, 9) = "</li><li>" Then strHTML = Left$(strHTML, Len(strHTML) - 9)
strHTML = strHTML & "</li></ul>"
End If
bInBullet = rtbRichTextBox.SelBullet
End If
strHTML = strHTML + rtbRichTextBox.SelText
Next
If Right$(strHTML, 14) = "<div align=left>" Then strHTML = Left$(strHTML, Len(strHTML) - 16)
If Right$(strHTML, 15) = "<div align=right>" Then strHTML = Left$(strHTML, Len(strHTML) - 17)
If Right$(strHTML, 16) = "<div align=center>" Then strHTML = Left$(strHTML, Len(strHTML) - 18)
If Left$(strHTML, 4) = "</div>" Then strHTML = Right$(strHTML, Len(strHTML) - 4)
strHTML = Replace(strHTML, "<div></div>", "<br>")
strHTML = Replace(strHTML, "<div align=left></div><div align=left></div>", "<br>")
strHTML = Replace(strHTML, "<div align=right></div><div align=right></div>", "<br>")
strHTML = Replace(strHTML, "<div align=center></div><div align=center></div>", "<br>")
If Right$(strHTML, 4) = "<li>" Then strHTML = Left$(strHTML, Len(strHTML) - 4) & "</ul></div>"
strHTML = "<HTML><HEAD><TITLE>" & sSubject & "</TITLE></HEAD><BODY>" & strHTML & "</BODY></HTML>"
RichToHTML = strHTML
End Function