Convert Lotus Notes Documents to PDFs with LotusScript and Microsoft Word

In some of our old databases we wanted to convert the NotesDocument itself to a PDF-A Documents for archiving purposes.

We did it until now by opening the NotesUIDocument in the client and converted it ot PDF-A with an PDF-Printer (PDF-Creator). But often these NotesUIDocuments did note closed correctly and at the end of the NotesAgent we had thousands of NotesUIDocuments opened in the Client…

We searched for another way to convert the NotesDocument. We tried to render it to an NotesRichtTextItem, converting it to an MimeItem (based on the work from Yuri (here…), generating some HTML. But the HTML had some glitches: Notes inserts a small transparent gif into empty table-cells, so we would have to deal with different MimeEntities wich is also a PITA.

Then I found some code (from Steven J. Knight) in the IBM-Forums wich called a Notes-API function to export Microsoft RTF (which is called by the @Command([ExportFile], „Microsoft RTF“, fileName)

This works as a charm, generating nice looking RTF-Files. Together with a Microsoft Word COM-Instance we convert it to PDA-A quite fast and reliable:

%REM
  Library PdfConverter
  Created 11.07.2013 by Harald Reisinger
  Description: Functions to generate RTF and PDF Documents from NotesDocuments
  Needs the OpenLog Databse for Logging Errors (http://www.openntf.org/internal/home.nsf/project.xsp?action=openDocument&name=OpenLog)
%END REM
Option Public
Option Declare

Use "OpenLogFunctions"

Const APIModule = "NNOTES" 

Const wdExportFormatPDF = 17
Const wdExportFormatXPS = 18
Const wdExportOptimizeForOnScreen = 1
Const wdExportOptimizeForPrint = 0
Const wdExportAllDocument = 0
Const wdExportCurrentPage = 2
Const wdExportFromTo = 3
Const wdExportSelection = 1
Const wdExportDocumentContent = 0
Const wdExportDocumentWithMarkup = 7
Const wdExportCreateHeadingBookmarks = 1
Const wdExportCreateNoBookmarks = 0
Const wdExportCreateWordBookmarks = 2

Declare Function MailGetMessageBodyComposite Lib APIModule Alias "MailGetMessageBodyComposite" ( ByVal hNT As Long, ByVal N As String, ByVal D As String, nD As Long) As Integer
Declare Function ExportRTF Lib "nxrtf" Alias "ExportRTF" (ByVal sTempFile As String, ByVal flags As Long, hmod As Long, ByVal altlibrary As String, ByVal sRTFFile As String) As Integer


%REM
  Sub ConvertDocToRtfFile
  Description: Converts a NotesDocument to an RTF-Document and saves it under the filePath
  Generates and removes some temporary Files while Working
%END REM
Public Sub ConvertDocToRtfFile(doc As NotesDocument, filePath As String)
  Dim tempDoc As NotesDocument
  Dim tempRti As NotesRichTextItem

  On Error GoTo ErrorHandler

  Set tempDoc = doc.ParentDatabase.CreateDocument()
  Set tempRti = tempDoc.CreateRichTextItem("Body")
  
  Call doc.RenderToRTItem(tempRti)
  Call ConvertItemToRtfFile(tempRti, filePath)	

  Exit Sub

ErrorHandler:
  Call LogErrorEx("Error in: ConvertDocToRtfFile", SEVERITY_HIGH, doc)
  Error Err, Error$


End Sub
%REM
  Sub ConvertDocumentToPdf
  Description: Converts a NotesDocument to an PDF-Document and saves it under the filePath
  Needs an COM-Instace of a Word Application created with:
    Dim word As Variant
    Set word = CreateObject("Word.Application")
  Generates and removes some temporary Files while Working
%END REM
Public Sub ConvertDocumentToPdf(doc As NotesDocument, word As Variant, filePath As String)
  Dim wordDoc As Variant
  
  On Error GoTo ErrorHandler

  Call ConvertDocToRtfFile(doc, filePath & ".rtf")
  'http://msdn.microsoft.com/en-us/library/office/bb216319(v=office.12).aspx
  Set wordDoc = word.Documents.Open(filePath & ".rtf", False, True, False)
  'http://msdn.microsoft.com/en-us/library/office/bb256835(v=office.12).aspx
  Call wordDoc.ExportAsFixedFormat(filePath, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument, 0, 9999999, wdExportDocumentContent, True, True,  wdExportCreateHeadingBookmarks, True, True, True, Nothing)
  Call wordDoc.Close(0)

  Kill filePath & ".rtf"

  Exit Sub

ErrorHandler:
  Call LogErrorEx("Error in: ConvertDocumentToPdf", SEVERITY_HIGH, doc)
  Error Err, Error$

End Sub

%REM
  Function ConvertItemToRtfFile
  Description: Converts an NotesRichTextItem to an RTF-Document and saves it under the filePath
  Generates and removes a temporary File while Working
%END REM
Public Sub ConvertItemToRtfFile(item As NotesRichTextItem, filePath As String)
  On Error GoTo ErrorHandler
  
  Dim fileSize As Long
  Dim doc As NotesDocument
  
  Set doc = item.Parent

  Call MailGetMessageBodyComposite(doc.handle , "Body", filePath & ".cd", fileSize) 
  Call ExportRTF(filePath & ".cd", 0, 0, "", filePath)
  
  Kill filePath & ".cd"

  Exit Sub

ErrorHandler:
  Call LogErrorEx("Error in: ConvertItemToRtfFile", SEVERITY_MEDIUM, doc)
  Error Err, Error$
End Sub