• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Convert Rich Tex to HTML code.

I am using the following code to transform the contents of a rtf box to html code, the only problem is it doesn't convert new lines. ie (vbCrLf should be converted to <br>)

Watch out for line wrap...

<<<<START FUNCTION>>>

Function RichToHTML(rtbRichTextBox As RichTextLib.RichTextBox, Optional lngStartPosition As Long, Optional lngEndPosition As Long) As String
'--------------------- The Arguments -----------------------
'rtbRichTextBox     - The rich textbox control to convert.
'lngStartPosition   - The character position to start from.
'lngEndPosition     - The character position to end at.
'-----------------------------------------------------------
'Returns:     The rich text converted to HTML.

Dim blnBold As Boolean, blnUnderline As Boolean, blnStrikeThru As Boolean
Dim blnItalic As Boolean, strLastFont As String, lngLastFontColor As Long
Dim strHTML As String, lngColor As Long, lngRed As Long, lngGreen As Long
Dim lngBlue As Long, lngCurText As Long, strHex As String, intLastAlignment As Integer
Dim intFontSize As Integer


Const AlignLeft = 0, AlignRight = 1, AlignCenter = 2

'check for lngStartPosition ad lngEndPosition

If IsMissing(lngStartPosition&) Then lngStartPosition& = 0
If IsMissing(lngEndPosition&) Then lngEndPosition& = Len(rtbRichTextBox.TextRTF)

lngLastFontColor& = -1 'no color

   For lngCurText& = lngStartPosition& To lngEndPosition&
       rtbRichTextBox.SelStart = lngCurText&
       rtbRichTextBox.SelLength = 1
   
          If intLastAlignment% <> rtbRichTextBox.SelAlignment Then
             intLastAlignment% = rtbRichTextBox.SelAlignment
             
                Select Case rtbRichTextBox.SelAlignment
                   Case AlignLeft: strHTML$ = strHTML$ & "<p align=left>"
                   Case AlignRight: strHTML$ = strHTML$ & "<p align=right>"
                   Case AlignCenter: strHTML$ = strHTML$ & "<p align=center>"
                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
            intFontSize = rtbRichTextBox.SelFontSize - 7
            strHTML$ = strHTML$ + "<font size=" & intFontSize & " 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
 
     strHTML$ = strHTML$ + rtbRichTextBox.SelText

   Next lngCurText&
   

RichToHTML = strHTML$


End Function




<<<END FUNCTION>>>
0
jimmyjoe
Asked:
jimmyjoe
  • 2
  • 2
  • 2
  • +1
1 Solution
 
nzjonboyCommented:
you could try searching through the entire string for vbCrLf and then replace it with <br>, or (probably better) you could read the rtb up to the vbCrLf add <br> skip the new line indicator and then read the next line repeating until then end. then run the code you already have on the newly compiled string.

hope this helps

nzjonboy
0
 
jimmyjoeAuthor Commented:
Could you give me a small example of how to do that? (ie. code.)

Thanks
0
 
jimmyjoeAuthor Commented:
I tried adding this line before the last line.

strHTML$ = Replace(strHTML$, vbCrLf, vbCrLf & "<br>")

but it doesn't work.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Richie_SimonettiIT OperationsCommented:
I think Replace function doesn't work because you are working with .TextRTF property, not plain .text property.
Try saving as:

strHTML$ = Replace(rtbRichTextBox.text, vbCrLf, vbCrLf & "<br>")

0
 
nzjonboyCommented:
jimmyjoe

using the code strHTML$ = Replace(strHTML$, vbCrLf, vbCrLf & "<br>") you are trying to replace vbCrLf with vbCrLf<br>, try this code instead

strHTML$ = Replace(strHTML$, vbCrLf, "<br>")

put it imediately before this line of code

RichToHTML = strHTML$

hope this helps

nzjonboy
0
 
ArkCommented:
Just a comment - for working at all platforms, use vbNewLine constant instead of vbCrLf.

Cheers
0
 
Richie_SimonettiIT OperationsCommented:
Ark, i did try your suggestion on a program by myself and vbnewline has the same efect as vbcrlf :(
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now