Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Convert RTF to HTML using MS Word programatically

Posted on 2008-10-06
Medium Priority
Last Modified: 2012-05-05
I have a VB6 legacy app with the following functionality:
1. Functions that retrieve RTF formatted documents as strings from a sql database.
2. Functions that parse through the RTF tags and insert additional data from the database.
3. One function that takes the final string and assigns it to the .body of an MS Outlook message.

This functionality, messy at best, was working fine until we switched from Outlook 2000 to Outlook 2003. Apparently Outlook 2003 doesn't support RichText, so now all the RTF tags are showing up in the email body.  It's not feasible at this point to update all the sql stored RTF documents or the first two functionality points mentioned above...so my only other option is to convert the RTF to HTML on the fly before sending it to Outlook.

I'VE TRIED MANY RTF TO HTML CONVERTERS inside the code and none of them convert the indentation and spacing correctly.  The application does have the ability to preview the outlook email message body before sending it by storing the RTF string inside a RichTextBox.  I manually copied the preview from the RichTextBox, pasted it in word and saved it as an HTML and it looks perfect.  I would basically like to do that programmatically.

I've searched far and wide and despite several forums on the subject, still couldn't find any VB6 examples of this.  

Any help would be greatly appreciated.

Thanks in advance!
Question by:wrsteam
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
  • 3
  • 3
LVL 25

Expert Comment

by:Luis Pérez
ID: 22650939
Try the code below, I'm sure it will work for you.
'Declare a constant with the HTML format for Word savings
Const wdFormatHTML As Integer = 8
'Declare an object for containing the word application
Dim wordApp As Object
'Declare an object for containing the word document
Dim wordDoc As Object
'Create the word application. This is done by CreateObject instead
'of adding a reference to the Word Object Library; if we add a 
'reference, we are making our VB application version-dependient of
'the Word version installed at that time; by using CreateObject,
'VB app is version-independient (the Word version installed doesn't
Set wordApp = CreateObject("Word.Application")
'Open the RTF document
Set wordDoc = wordApp.Documents.Open("complete_path_to_the_rtf_document")
'Save the document in HTML format
wordDoc.SaveAs "complete_path_of_the_html_document", wdFormatHTML
'Free memory & resources by closing objects

Open in new window


Author Comment

ID: 22651554
I appreciate the solution, however, as I mentioned in my original post:

The RTF doc is in the form of a string with RTF tags...so it's not a file and I therefore have no path.

That said, the line of code you proposed will not work:

Set wordDoc = wordApp.Documents.Open("complete_path_to_the_rtf_document")

Any other ideas?
LVL 25

Expert Comment

by:Luis Pérez
ID: 22657017
If you have the possibility, you can work with temp files. First, save your RTF string data into a temp file; then, use MS Word to convert into HTML with the code provided in my previous post; last, if you need it, open the HTML file and get the content into a string in order to set as body of your e-mail. Finally, delete both temp files (.rtf and .html).
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 22658902
Okay, but I don't know how to get the content from the string into the temp file...nor do I know how to get the content of the html file back into a string.  Can you please provide some sample code that does this?

Thanks so much.  You've been extremely helpful!!  
LVL 25

Accepted Solution

Luis Pérez earned 2000 total points
ID: 22659564
Here is the code. Hope that helps.
'Function that reads entire content of a file (in any format) to a string
Public Function ReadFile(ByVal Filename As String) As String
    'Create a handle for the file
    Dim hFile As Integer
    'A buffer to hold the readed data
    Dim sBuffer As String
    'Obtain a free handle to a file
    hFile = FreeFile
    'Open the file for binary access mode for reading
    Open Filename For Binary Access Read As hFile
    'Initialize the buffer to the same number of white spaces
    'as bytes are in the file to read
    sBuffer = Space(LOF(hFile))
    'Fill the buffer with the data readed from the file
    Get #hFile, , sBuffer
    'Close the file
    Close hFile
    'Return the buffer
    ReadFile = sBuffer
End Function
'Function that writes the contents of a string to a file
Public Sub WriteFile(ByVal Filename As String, ByVal Data As String)
    'Create a handle for the file
    Dim hFile As Integer
    'Obtain a free handle to a file
    hFile = FreeFile
    'Open the file for binary access mode for writing. If the file
    'doesn't exists, it will be created.
    Open Filename For Binary Access Write As hFile
    'Write the data to the file
    Put #hFile, , Data
    'Close the file
    Close hFile
End Sub

Open in new window


Author Closing Comment

ID: 31503433
AMAZING.  I can't even begin to tell you how helpful your solution has been!!!  I have literally been pulling my hair out trying to figure this out...not to mention the grief I'm getting from my client.  You are truly an EXPERT!


Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

618 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