Convert large number of documents

This LotusScript agent code converts all documents from view in defined database. The code is suitable for views with large amount of documents.


Use "SwPDFMain"

Dim s As NotesSession
Dim db As NotesDatabase

Sub Initialize
	Dim view As NotesView
	Dim doc As NotesDocument
	Dim swPDF As SwPDFCreator, swPDFDoc As SwPDFDocument, swPDFErr As SwPDFError
	Dim OutputFilePath As String
	Dim counter As Long
	
	On Error GoTo Ooops
	
	Set s = New NotesSession
	MsgBox "Agent started!!"

	'Leave this if current db should be used 
	Set db = s.CurrentDatabase
	
	'Use this function to open remote database 
	'Set db = s.Getdatabase("Server", "File", Createonfail)
	
	' Get documents from this database view
	Set view = db.Getview("(($All))")
    Counter = 0
    
	Set swPDF = New SwPDFCreator()
	swPDF.Init("LICENCE KEY HERE")	
	swPDF.PDFSettings.useLowLevelRenderer = True
	
	'Set this to false to avodid download of Internet content.
	'This includes HTML newsletter images and other data that might slow down conversion.
	'Image placeholder is shown instead of real image. 
	swPDF.PDFSettings.EnableHTTPStream = true
	
	Set doc = view.Getfirstdocument()
	While Not(doc Is Nothing)
		Counter = Counter + 1
		
		MsgBox "Processing document " & Counter & " / " & view.Entrycount

		' ***************Set your file path here!*********************
		OutputFilePath = "d:\convertedPDF\" & doc.UniversalID & ".pdf"	
		'*************************************************************		
		Set swPDFDoc = swPDF.ProcessDocument(doc)
		
		If(Not swPDFDoc Is Nothing) Then
			Call swPDFDoc.SaveToFile(OutputFilePath)
		Else
			' There was an error in PDF conversion
			Set swPDFErr = swPDF.GetError()
		End If
		
		'File verification
		If FileExists(OutputFilePath) = false Then
			' There is no output file, so let's log this
			Call logError("File verification failed", doc)
		End If
		
		Set doc = view.GetNextDocument(doc)
	Wend
	Exit Sub

Ooops:
	Msgbox "Error on line " & Erl & " Description: " & Error
	Resume Next
End Sub

Function logError(ErrMsg As String, docSrc As NotesDocument) As Boolean
	Dim docLog As NotesDocument
	
	Set docLog = db.CreateDocument
	
	Call docLog.ReplaceItemValue("Form", "SwErrorLog")
	Call docLog.ReplaceItemValue("ErrorMessage", ErrMsg)
	
	If Not docSrc Is Nothing Then
		Call docLog.ReplaceItemValue("SourceDbServer", docSrc.ParentDatabase.Server)
		Call docLog.ReplaceItemValue("SourceDbPath", docSrc.ParentDatabase.FilePath)
		Call docLog.ReplaceItemValue("SourceDbTitle", docSrc.ParentDatabase.Title)
		Call docLog.ReplaceItemValue("SourceDocUNID", docSrc.UniversalID)
		Call docLog.ReplaceItemValue("SourceDocForm", docSrc.Form(0))
'		docLog.replaceItemValue("AlternateForm", SwPDFSettings.PDFAlternateForm)
	End If
		
	Call docLog.ReplaceItemValue("User", s.UserName)
'	docLog.replaceItemValue("PDFCVersion", SwPDFSettings.ProductVersion);
	Call docLog.ReplaceItemValue("NotesVersion", s.NotesVersion)
'	docLog.replaceItemValue("ConversionType", conversionType);
		
	Call docLog.save(True, True)
End Function


Function FileExists(sFileName As String) As Integer
 'Declare variables..
	Dim iPos As Integer, iPrevPos As Integer
	Dim sFile As String
	
 'Validate passed filename...
	iPos = 0
	Do
		iPrevPos = iPos
		iPos = InStr(iPos + 1, sFileName, "\")
		MsgBox iPos
	Loop Until iPos = 0
	iPos = iPrevPos
	
	sFile = Right$(sFileName, Len(sFileName) - iPos)
	
 'Check to see if file exists and return results...
	If Dir$(sFileName) = sFile Then
		FileExists = True
	Else
		FileExists = False
	End If
End Function