Solved

Convert Rich Tex to HTML code.

Posted on 2001-08-09
7
253 Views
Last Modified: 2008-02-01
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
Comment
Question by:jimmyjoe
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6370568
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
 

Author Comment

by:jimmyjoe
ID: 6370851
Could you give me a small example of how to do that? (ie. code.)

Thanks
0
 

Author Comment

by:jimmyjoe
ID: 6370861
I tried adding this line before the last line.

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

but it doesn't work.

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 200 total points
ID: 6374314
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
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6378162
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
 
LVL 27

Expert Comment

by:Ark
ID: 6378247
Just a comment - for working at all platforms, use vbNewLine constant instead of vbCrLf.

Cheers
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6423206
Ark, i did try your suggestion on a program by myself and vbnewline has the same efect as vbcrlf :(
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now