title: Lotus Notes - Export emails to plain text file
body: <p>I am setting up a Lotus Notes account to accept emails from a client, and automatically save each email as a plain text file to be processed by another application.</p>
  
  <p>So, I'm trying to create my very first Agent in Lotus to automatically export the emails to text.</p>
  
  <p>Is there a standard, best practices way to do this?</p>
  
  <p>I've created a LotusScript Agent that pretty much works.  However, there is a bug - once the Body of the memo exceeds 32K characters, it starts inserting extra CR/LF pairs.</p>
  
  <p>I am using Lotus Notes 7.0.3.</p>
  
  <p>Here is my script:</p>
  
  <pre><code> Sub Initialize
   On Error Goto ErrorCleanup
   Dim session As New NotesSession
   Dim db As NotesDatabase
   Dim doc As NotesDocument
   Dim uniqueID As Variant 
   Dim curView As NotesView
   Dim docCount As Integer
   Dim notesInputFolder As String 
   Dim notesValidOutputFolder As String
   Dim notesErrorOutputFolder As String 
   Dim outputFolder As String
   Dim fileNum As Integer
   Dim bodyRichText As NotesRichTextItem
   Dim bodyUnformattedText As String
   Dim subjectText As NotesItem
  
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''
   'INPUT OUTPUT LOCATIONS 
   outputFolder = "\\PASCRIA\CignaDFS\CUser1\Home\mikebec\MyDocuments\"
   notesInputFolder = "IBEmails" 
   notesValidOutputFolder = "IBEmailsDone"
   notesErrorOutputFolder="IBEmailsError"
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''
  
   Set db = session.CurrentDatabase
   Set curview = db.GetView(notesInputFolder ) 
   docCount = curview.EntryCount
   Print "NUMBER OF DOCS "  &amp; docCount
   fileNum = 1
   While (docCount &gt; 0)
    'set current doc to 
    Set doc = curview.GetNthDocument(docCount)
  
    Set bodyRichText = doc.GetFirstItem( "Body" )
    bodyUnformattedText = bodyRichText.GetUnformattedText()
    Set subjectText = doc.GetFirstItem("Subject")
    If subjectText.Text = "LotusAgentTest" Then
     uniqueID = Evaluate("@Unique")
     Open "\\PASCRIA\CignaDFS\CUser1\Home\mikebec\MyDocuments\email_" &amp; uniqueID(0) &amp; ".txt" For Output As fileNum
     Print #fileNum, "Subject:" &amp; subjectText.Text
     Print #fileNum, "Date:" &amp; Now
     Print #fileNum, bodyUnformattedText
     Close fileNum
     fileNum = fileNum + 1
     Call doc.PutInFolder(notesValidOutputFolder)
     Call doc.RemoveFromFolder(notesInputFolder)
    End If
    doccount = doccount-1
   Wend
   Exit Sub
      ErrorCleanup: 
       Call sendErrorEmail(db,doc.GetItemValue("From")(0))
       Call doc.PutInFolder(notesErrorOutputFolder)
       Call doc.RemoveFromFolder(notesInputFolder)
      End Sub
  </code></pre>
  
  <p><strong>Update</strong>
  Apparently the 32KB issue isn't consistent - so far, it's just one document that starts getting extra carriage returns after 32K.</p>