Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert Rich Tex to HTML code.

Posted on 2001-08-09
7
256 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 28

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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