'Doc To HTML: Option Public Option Explicit %REM This agent is an example of using the Notes API calls that were made public in Notes 7.0.2 to convert a NotesDocument to an HTML string. I have no idea how backwards-compatible this is for Notes versions prior to 7.0.2, but it does seem to work in Notes 8.0. A few items of note: * Hide-when formulas may or may not work as expected (i.e. -- some things that you think should be hidden might show up in the HTML, or vice versa, especially things based on roles, user names, or groups) * Images and file references are output as relative links in the HTML, rather than being MIME-encoded somehow * The first run of this code on a workstation sometimes takes a few seconds for me (possibly loading up http in the background or something, I don't know) THIS CODE IS PROVIDED AS-IS. USE AT YOUR OWN RISK. THIS IS AN EXAMPLE ONLY. version 1.0 15 July 2008 Julian Robichaux -- http://www.nsftools.com %END REM Declare Function OSPathNetConstruct Lib "NNOTES" Alias "OSPathNetConstruct" _ ( Byval NullPort As Long, Byval Server As String, Byval FIle As String, _ Byval PathNet As String) As Integer Declare Function NSFDbOpen Lib "NNOTES" Alias "NSFDbOpen" _ ( Byval PathName As String, DbHandle As Long) As Integer Declare Function NSFDbClose Lib "NNOTES" Alias "NSFDbClose" _ ( Byval DbHandle As Long) As Integer Declare Function HTMLCreateConverter Lib "NNOTES" Alias "HTMLCreateConverter" _ ( HtmlHandle As Long) As Integer Declare Function HTMLDestroyConverter Lib "NNOTES" Alias "HTMLDestroyConverter" _ ( Byval HtmlHandle As Long) As Integer Declare Function HTMLConvertNote Lib "NNOTES" Alias "HTMLConvertNote" _ ( Byval HtmlHandle As Long, Byval DbHandle As Long, Byval NoteHandle As Long, _ Byval UrlArgsCount As Long, Byval NullUrlArgs As Long) As Integer Declare Function HTMLGetPropertyLong Lib "NNOTES" Alias "HTMLGetProperty" _ ( Byval HtmlHandle As Long, Byval PropertyType As Long, RetVal As Long) As Integer Declare Function HTMLGetText Lib "NNOTES" Alias "HTMLGetText" _ ( Byval HtmlHandle As Long, Byval StartingOffset As Long, TextLength As Long, _ Byval RetVal As Lmbcs String) As Integer '** Error code masks Const ERR_MASK = &H3fff Const PKG_MASK = &H3f00 Const ERRNUM_MASK = &H00ff Declare Function OSLoadString Lib "nnotes.dll" (Byval hModule As Long, Byval stringCode As Integer, _ Byval retBuffer As String, Byval bufferLength As Integer) As Integer Function ConvertDocToHtml (doc As NotesDocument) As String %REM This function uses Notes API calls that were made public in release 7.0.2 to convert a NotesDocument to HTML. Please download the Notes 8.0 C-API Toolkit and look at the Reference Guide database for more information. Note that any errors are returned abruptly using MessageBox; that's really not the best way to handle errors. Sorry about all the Goto statements. When you're working with API handles that MUST be closed, it's sometimes easier this way. %END REM '** create a proper network path name with OSPathNetConstruct Dim db As NotesDatabase Dim hDb As Long Dim pathName As String Set db = doc.ParentDatabase pathName = String(256, " ") Call OSPathNetConstruct(0, db.Server, db.FilePath, pathName) '** open the database and get a handle with NSFDbOpen (if you're doing this '** to a lot of documents, you should try to only open the database once) Dim result As Integer result = NSFDbOpen(pathName, hDb) If result <> 0 Then Messagebox "Cannot open database " & db.FilePath & " on server " & db.Server & _ ". Error was " & Cstr(result) & ": " & GetAPIError(result) Exit Function End If '** create an HTML Converter Dim converter As Long result = HtmlCreateConverter(converter) If result <> 0 Then Messagebox "Cannot create HTML Converter" & _ ". Error was " & Cstr(result) & ": " & GetAPIError(result) Goto closeDb End If '** convert the doc to HTML (I'm taking a shortcut with doc.Handle here; '** you may want to consider using NSFNoteOpen or similar) result = HtmlConvertNote(converter, hDB, doc.Handle, 0, 0) If result <> 0 Then Messagebox "Cannot convert note " & doc.UniversalID & " to HTML" & _ ". Error was " & Cstr(result) & ": " & GetAPIError(result) Goto destroyConverter End If '** figure out how long the resulting HTML is Dim textLength As Long result = HtmlGetPropertyLong(converter, 0, textLength) If result <> 0 Then Messagebox "Cannot determine HTML Converter text length" & _ ". Error was " & Cstr(result) & ": " & GetAPIError(result) Goto destroyConverter End If '** send the converted HTML to a string Dim finalString As String Dim chunk As String Dim chunkSize As Long Dim startPos As Long chunkSize = 1024 '** adjust chunkSize to meet your comfort level Do While (startPos < textLength) If ((textLength - startPos) < chunkSize) Then chunkSize = textLength - startPos End If chunk = String(chunkSize, " ") result = HtmlGetText(converter, startPos, chunkSize, chunk) If result <> 0 Then Messagebox "Cannot get HTML text between " & startPos & " and " & (startPos + chunkSize) & _ ". Error was " & Cstr(result) & ": " & GetAPIError(result) Goto destroyConverter End If finalString = finalString & Left(chunk, chunkSize) startPos = startPos + chunkSize Loop ConvertDocToHtml = finalString '** this is API work, so make sure you do the appropriate cleanup when you're done destroyConverter: Call HTMLDestroyConverter(converter) closeDb: Call NSFDbClose(hDb) endOfFunction: Exit Function End Function Sub Initialize '** as an example, get the first doc in this database and print the HTML Dim session As New NotesSession Dim doc As NotesDocument Dim html As String Set doc = session.CurrentDatabase.AllDocuments.GetFirstDocument html = ConvertDocToHtml(doc) Print html End Sub Function GetAPIError (errorCode As Integer) As String '** this function translates Notes API error codes into their '** corresponding error strings Dim errorString As String*256 Dim returnErrorString As String Dim resultStringLength As Long Dim errorCodeTranslated As Integer '** mask off the top 2 bits of the errorCode that was returned; this is '** what the ERR macro in the API does errorCodeTranslated = (errorCode And ERR_MASK) '** get the error code translation using the OSLoadString API function resultStringLength = OSLoadString(0, errorCodeTranslated, errorString, Len(errorString) - 1) '** strip off the null-termination on the string before you return it If (Instr(errorString, Chr(0)) > 0) Then returnErrorString = Left$(errorString, Instr(errorString, Chr(0)) - 1) Else returnErrorString = errorString End If GetAPIError = returnErrorString End Function