Domino Code Fragment

Code Name*
GetHTMLFromRTF
Date*
11/30/2000
Source (or email address if you prefer)*
Shane Hollis
IP address:.203.98.9.62
Description*
Gets an HTML string from a RTF field using undocumented methods in Lotus notes for version 5.02 onwards.
Type*
LotusScript
Categories*
Implementation:
Modify code
Required Client:
5.1
Server:
5.1
Limitations:
Needs to run in R 5.02 upwards.
Comments:
Code example and class listing for the NotesMimeEntity class to parse HTML or Mime from a rich text field. Tested and ran ok.
Files/Graphics attachments (if applicable): Code:
In Notes version 5x you can save a Rich Text Field (RTF) as HTML/MIME. This is very useful if you want to render your rich text on the web.
Unfortunately getting the html from the Rich text is almost impossible in the client...until now.

From versiob 5.02 upwards Lotus has a class called NotesMimeEntity that will allow you to get the html/mime code from the RF field. This is not documented anywhere in the help files. In order to find it and get it's methods / properties go to your script editor and open up the programmers pane. Under the reference tab find Domino Classes and look for NotesMimeEntity.

Its properties are:
String ContentAsText
String ContentSubType
String ContentType
String Headers

Its methods are:
GetFirstChildEntity() as NotesMimeEntity
GetNextSibling() as NotesMimeEntity
GetParentEntity() as NotesMimeEntity

This class is undocumented and doesn't seem to have any Lotus support for it as all questions in the FAQ's , enquiries etc remain un-answered.

One other helpful undocumented method is:
Boolean NotesSession.ConvertMime

This either allows or disallows conversion of text and mime.

Below is an example of some code for accessing these methods and getting the html from a rich text field called RTF. To run it place the code in a button of a form with a field called RTF of type richText. Set the RTF fields store property to 'Store contents as HTML / Mime'. This setting is on the second tab of the field properties in designer. Make a second field called HTMLText, of type text, on the form. The button will open a msgBox showing the html then populate the text field with the html generated.
Populate the RTF field with Rich text, tables etc then save the document, click on the button and hey presto...instant html. Make sure the doc is in edit mode for this button to work else it will error when it tries to set the HTMLText field.

Have fun.

Option Explicit


Const MIME_TYPE_MULTIPART = "multipart"
Const MIME_TYPE_TEXT = "text"
Const MIME_SUBTYPE_RELATED = "related"
Const MIME_SUBTYPE_HTML = "html"
Const HTML_START = "<HTML>"
Const HTML_FINISH = "</HTML>"
Const HTML_HEADER_START = "<HEAD>"
Const HTML_HEADER_FINISH = "</HEAD>"
Const HTML_BODY_START = "<BODY>"
Const HTML_BODY_FINISH = "</BODY>"
Const HTML_REM_START = "<!-- "
Const HTML_REM_FINISH = " --> "
Const MB_ICONINFORMATION = 64
Const MB_ICONEXCLAMATION = 48


Function GetHtmlFromRTF(thisDoc As Notesdocument, RTFName$) As String
On Error Goto errorHandle

Dim session As New NotesSession
Dim item As NotesItem
Dim mime As NotesMIMEEntity
Dim childMime As NotesMimeEntity
Dim returnString$


Set item = thisDoc.GetFirstItem( RTFName$ )
Set mime = item.GetMimeEntity

session.ConvertMime = False

Select Case Lcase(mime.ContentType)

Case MIME_TYPE_MULTIPART
returnString = GetMultiPartMimeAsHTML(mime)

Case MIME_TYPE_TEXT
returnString$ = HTML_START + Chr$(10)
returnString$ = returnString$ + HTML_HEADER_START + Chr$(10)
returnString$ = returnString$ + Chr$(10) + HTML_REM_START + Chr$(10)
returnString$ = returnString$ + mime.Headers
returnString$ = returnString$ + Chr$(10) + HTML_REM_FINISH + Chr$(10)
returnString$ = returnString$ + Chr$(10) + HTML_HEADER_FINISH
returnString$ = returnString$ + HTML_BODY_START + Chr$(10)
returnString$ = returnString$ + mime.ContentAsText
returnString$ = returnString$ + Chr$(10) + HTML_BODY_FINISH
returnString$ = returnString$ + Chr$(10) + HTML_FINISH
Case Else
returnString$ ="Unknown"

End Select ' mime.ContentType

Set childMime = mime.GetFirstChildEntity

GetHtmlFromRTF = returnString$

Exit Function

'******************** Error handling *****************
errorHandle:

Msgbox "There has been an error: " + Chr$(13) + "Number: " + Cstr(Err) + Chr$(13) + "Message: " + Error$, _
MB_ICONEXCLAMATION,_
"Error in [GetHTMLFromRTF]"

GetHtmlFromRTF = "Error"

Exit Function
End Function



Function GetMultiPartMimeAsHTML(parentMime As NotesMimeEntity )
On Error Goto errorHandle

Dim returnString$, headers$, contentText$
Dim childMime As NotesMimeEntity

Set childMime = parentMime.GetFirstChildEntity

While Not(childMime Is Nothing)
headers$ = headers$ + childMime.Headers
contentText$ = contentText$ + childMime.ContentAsText
Set childMime = parentMime.GetNextsibling()
Wend

returnString$ = HTML_START + Chr$(10)
returnString$ = returnString$ + HTML_HEADER_START + Chr$(10)
returnString$ = returnString$ + HTML_REM_START + Chr$(10)
returnString$ = returnString$ + headers$
returnString$ = returnString$ + Chr$(10) + HTML_REM_FINISH + Chr$(10)
returnString$ = returnString$ + Chr$(10) + HTML_HEADER_FINISH + Chr$(10)
returnString$ = returnString$ + Chr$(10) + HTML_BODY_START + Chr$(10)
returnString$ = returnString$ + contentText$
returnString$ = returnString$ + Chr$(10) + HTML_BODY_FINISH
returnString$ = returnString$ + Chr$(10) + HTML_FINISH

GetMultiPartMimeAsHTML = returnString$

Exit Function

'******************** Error handling *****************
errorHandle:

Msgbox "There has been an error: " + Chr$(13) + "Number: " + Cstr(Err) + Chr$(13) + "Message: " + Error$, _
MB_ICONINFORMATION,_
"Error in [GetMultiPartMimeAsHTML]"

GetMultiPartMimeAsHTML = "Error"

Exit Function
End Function

Sub Click(Source As Button)
Dim workspace As New NotesUiWorkspace
Dim doc As NotesDocument
Dim uiDoc As NotesUidocument
Dim html$

Set uiDoc = workspace.currentdocument
Set doc = uiDoc.Document

html$ = GetHTMLFromRTF(doc, "RTF")
Msgbox html$, MB_ICONINFORMATION, "HTML from RTF Field"
Call uiDoc.FieldSetText("HTMLText", html$)
End Sub


To do this on the web there is an undocumented / unsupported applet .

It is found at...
http://support.lotus.com/lshome.nsf

Search for : How to Get MIME Information Into a Text Field From the Rich Text Applet
Document #: 176696

Applet API is at: http://www.notes.net/46dom.nsf/aac7d56ca8fd884b852563be00610639/2490f16321680ec98525678a0049f2fe?OpenDocument