Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert RTF to HTML? 1

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
Hi,

I want to take a message written in a VB RTF box and convert it to HTML so that it can be emailed.

I've found that EasyByte do a $149 RTF to HTML converter but I can't believe that if in VB you have a form with an RTF box and WebBrowser object that you can't somehow convert RTF to HTML?

Any ideas anyone?


- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Hi Andy,

Haven't tested this, but found it on the net.

Code:
Public Function ConvertToHTML(ByVal Box As RichTextBox) _
               As String
   ' Takes a RichTextBox control and returns a
   ' simple HTML-formatted version of its contents
   Dim strHTML As String
   Dim strColour As String
   Dim blnBold As Boolean
   Dim blnItalic As Boolean
   Dim strFont As String
   Dim shtSize As Short
   Dim lngOriginalStart As Long
   Dim lngOriginalLength As Long
   Dim intCount As Integer
   ' If nothing in the box, exit
   If Box.Text.Length = 0 Then Exit Function
   ' Store original selections, then select first character
   lngOriginalStart = 0
   lngOriginalLength = Box.TextLength
   Box.Select(0, 1)
   ' Add HTML header
   strHTML = "<html>"
   ' Set up initial parameters
   strColour = Box.SelectionColor.ToKnownColor.ToString
   blnBold = Box.SelectionFont.Bold
   blnItalic = Box.SelectionFont.Italic
   strFont = Box.SelectionFont.FontFamily.Name
   shtSize = Box.SelectionFont.Size
   ' Include first 'style' parameters in the HTML
   strHTML += "<span style=""font-family: " & strFont & _
     "; font-size: " & shtSize & "pt; color: " _
                     & strColour & """>"
   ' Include bold tag, if required
   If blnBold = True Then
       strHTML += "<b>"
   End If
   ' Include italic tag, if required
   If blnItalic = True Then
       strHTML += "<i>"
   End If
   ' Finally, add our first character
   strHTML += Box.Text.Substring(0, 1) 
   ' Loop around all remaining characters
   For intCount = 2 To Box.Text.Length
       ' Select current character
       Box.Select(intCount - 1, 1)
       ' If this is a line break, add HTML tag
       If Box.Text.Substring(intCount - 1, 1) = _
              Convert.ToChar(10) Then
           strHTML += "<br>"
       End If
       ' Check/implement any changes in style
       If Box.SelectionColor.ToKnownColor.ToString <> _
          strColour _ Or Box.SelectionFont.FontFamily.Name _
          <> strFont Or _ Box.SelectionFont.Size <> shtSize _
          Then
           strHTML += "</span><span style=""font-family: " _
             & Box.SelectionFont.FontFamily.Name & _
             "; font-size: " & Box.SelectionFont.Size & _
             "pt; color: " & _
             Box.SelectionColor.ToKnownColor.ToString & """>"
       End If
       ' Check for bold changes
       If Box.SelectionFont.Bold <> blnBold Then
           If Box.SelectionFont.Bold = False Then
               strHTML += "</b>"
           Else
               strHTML += "<b>"
           End If
       End If
       ' Check for italic changes
       If Box.SelectionFont.Italic <> blnItalic Then
           If Box.SelectionFont.Italic = False Then
               strHTML += "</i>"
           Else
               strHTML += "<i>"
           End If
       End If
       ' Add the actual character
       strHTML += Mid(Box.Text, intCount, 1)
       ' Update variables with current style
       strColour = Box.SelectionColor.ToKnownColor.ToString
       blnBold = Box.SelectionFont.Bold
      blnItalic = Box.SelectionFont.Italic
       strFont = Box.SelectionFont.FontFamily.Name
       shtSize = Box.SelectionFont.Size
   Next
   ' Close off any open bold/italic tags
   If blnBold = True Then strHTML += ""
   If blnItalic = True Then strHTML += ""
   ' Terminate outstanding HTML tags
   strHTML += "</span></html>"
   ' Restore original RichTextBox selection
   Box.Select(lngOriginalStart, lngOriginalLength)
   ' Return HTML
   Return strHTML
End Function

Hope it helps.

Wraygun

***You can't change your past, but you can change your future***
 
Sorry about that, I think this is a .NET example. It may port over pretty easy though.

Wraygun

***You can't change your past, but you can change your future***
 
Wraygun
Rather than copy & paste someone else's code you can just provide a reference so that the original author gets some credit. This appears to be copied from:

As you note this is VB.NET code and will require some amount of re-writing (almost all in fact)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
>I can't believe that if in VB you have a form with an RTF box and WebBrowser object that you can't somehow convert RTF to HTML?


Yep, you can. And basically all you have to do is copy from one (e.g. Clipboard.SetText RichTextBox1.TextRTF, rtfCFRTF) and paste to the other...
 
Thanks, I'll look into both approaches.

Strongm; I tried doing what you suggested but I can't send a WM_Paste message to the WebBroswer object, it says that the 'HWND method of iWebBrowser2 failed'. Is it possible to get around that?


- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Yes. Paste into a DHTML control rather than a webbrowser (assuming your setup actually has the DHTML controls)
 
Neither of the DHTML objects has a hWnd property...


- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
They do however expose the HTMLDocument interface, which allows the following:

myDoc.ExecCommand "selectall"
myDoc.ExecCommand "paste"

which should replace all the contents of the DHTML document with whatever is on the clipboard...
 
Thanks!

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top